This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) fetchThingsWithCompletion:(void (^)(NSArray *, NSError *))completion{ | |
[context performBlock:^{ | |
result = [context executeFetch:... | |
if (completion != nil){ | |
completion(result, error); | |
} | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group = dispatch_group_create(); | |
dispatch_group_enter(group); | |
[context performBlock:^{ | |
result = [context executeFetch:... | |
dispatch_group_leave(group); | |
}]; | |
groupResult = dispatch_group_wait(group, DISPATCH_TIME_FOREVER); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; | |
[context setParentContext:someOtherContext]; | |
result = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[[self fetchedResultsController] managedObjectContext] performBlock:^{ | |
NSError *fetchError = nil; | |
if (![self fetchedResultsController] performFetch:&fetchError]){ | |
/// handle the error. Don't just log it. | |
} else { | |
// Update the view from the main queue. | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
[tableView reloadData]; | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
[[self tableView] endUpdates]; | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSManagedObject *someObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; | |
value = [someObject lastName]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSManagedObject *someObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; | |
[[someObject managedObjectContext] performBlock:^{ | |
value = [someObject lastName]; | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { | |
NSManagedObject *row = [[self fetchedResultsController] objectAtIndexPath:indexPath]; | |
[[row managedObjectContext] performBlock:^{ | |
NSString *text = nil; | |
// Fire a fault. | |
text = [row lastName]; | |
// Update the user interface with the value. | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
NSManagedObjectContext *rootContext = nil; | |
// Override point for customization after application launch. | |
// 1. Create an NSManagedObjectContext with private queue concurrency | |
rootContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; | |
// 2. Instantiate a concrete NSPersistentStore by adding it to the coordinator asynchronously. | |
[rootContext performBlock:^{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Informal protocol that traverses the responder chain until a concrete class provides | |
an instance of NSManagedObjectContext. If no responder provides on, UIApplication is extended to forward | |
the request to it's delegate. We extend the UIApplicationDelegate to add this new behavior. | |
Concrete responders can provide optional implementations of managedObjectContext. For example, a responder such as | |
a view may call [self managedObjectContext], which would start walking up the responder chain. When it gets to the view controller that owns the view, the view controller may have a concrete implementation of managedObjectContext. This can be a property, or a method that creates a new context, etc. The instance vended by the view controller method is what the view that called [self managedObjectContext] gets as a response from that method. | |
Both the responder chain and informal protocols are incredible powerful concepts that are often under utilized by developers outside of Apple. |