Skip to content

Instantly share code, notes, and snippets.

View quellish's full-sized avatar

Dan quellish

View GitHub Profile
@quellish
quellish / fetchThingsWithCompletion.m
Created August 9, 2014 20:07
Pseudo-sample of a core data fetch with a completion block.
- (void) fetchThingsWithCompletion:(void (^)(NSArray *, NSError *))completion{
[context performBlock:^{
result = [context executeFetch:...
if (completion != nil){
completion(result, error);
}
}];
}
@quellish
quellish / waitForBlock.m
Created August 9, 2014 20:09
Pseudo-sample of synchronous use of Core Data performBlock: using dispatch group
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);
@quellish
quellish / frcQ.m
Created August 9, 2014 20:12
NSFetchedResultsController private queue context
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setParentContext:someOtherContext];
result = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
@quellish
quellish / asyncFetchFRC.m
Created August 9, 2014 20:14
Asynchronous fetch with an NSFetchedResultsController
[[[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];
}];
@quellish
quellish / controllerDidChangeContent.m
Created August 9, 2014 20:17
controllerDidChangeContent
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[self tableView] endUpdates];
}];
}
@quellish
quellish / incorrectPropertyAccess.m
Created August 9, 2014 20:20
The wrong way to access a Core Data property
NSManagedObject *someObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
value = [someObject lastName];
@quellish
quellish / correctPropertyAccess.m
Created August 9, 2014 20:23
Correct Core Data property access
NSManagedObject *someObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[someObject managedObjectContext] performBlock:^{
value = [someObject lastName];
}];
@quellish
quellish / willDisplayCell.m
Created August 9, 2014 20:26
Concrete example of correct property access pattern for CoreData
- (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:^{
@quellish
quellish / coreDataDidFinishLaunchingWithOptions.m
Created August 9, 2014 23:59
Asynchronous Core Data application:didFinishLaunchingWithOptions:
- (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:^{
@quellish
quellish / Foo.m
Created August 29, 2014 05:58
UIResponder Informal Protocol
/**
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.