Skip to content

Instantly share code, notes, and snippets.

@mikeabdullah
Created March 26, 2012 19:42
Show Gist options
  • Save mikeabdullah/2209110 to your computer and use it in GitHub Desktop.
Save mikeabdullah/2209110 to your computer and use it in GitHub Desktop.
Safe cross-thread passing of an error
- (BOOL)saveAndReturnError:(NSError **)error
{
NSManagedObjectContext *context = [self managedObjectContext];
__block BOOL result;
[context performBlockAndWait:^{
result = [context save:error];
if (!result && error) [*error retain];
}];
if (!result && error) [*error autorelease];
return result;
}
@alanjrogers
Copy link

I think it is fine because (NSError *_) implies (_autoreleasing NSError) under ARC. And the block should capture the pointer to the actual NSError object that has a strong reference as a local variable to the caller. (and it seems to work fine in my tests :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment