Skip to content

Instantly share code, notes, and snippets.

View quellish's full-sized avatar

Dan quellish

View GitHub Profile
1. Add two methods to handle the notification. You want to create and add a new store when the old one has been removed.
- (void) didChangePersistentStores:(id)notification {
NSArray *addedStores = nil;
NSArray *removedStores = nil;
NSArray *uuidChangedStores = nil;
addedStores = [[notification userInfo] valueForKey:NSAddedPersistentStoresKey];
removedStores = [[notification userInfo] valueForKey:NSRemovedPersistentStoresKey];
uuidChangedStores = [[notification userInfo] valueForKey:NSUUIDChangedPersistentStoresKey];
@quellish
quellish / gist:1e09834a9ff0ec95a34d
Created July 23, 2014 16:19
recursive context save
-(void) recursiveSave:(NSManagedObjectContext *)context {
NSError *error = nil;
[context performBlock:{
if (![context save:&error]){
[self errorHandler:error];
} else {
[self recursiveSave:[context parentContext]];
}
}];
}
@quellish
quellish / kvo
Last active August 29, 2015 14:04
correct kvo observe value for key path
// in the initializer, or viewWillAppear,etc:
[self addObserver:self forKeyPath:keyPath options:options context:(__bridge void*)self];
// in dealloc, or viewDidDisappear, etc:
[self removeObserver:self forKeyPath:keyPath context:(__bridge void*)self];
- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {
if ((__bridge id)context == self){
@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 / 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 / zomgwtf.m
Created August 31, 2014 01:10
2441856
// In your answer, you have this:
NSManagedObjectID *objectID = [person objectID];
dispatch_async(dispatch_get_main_queue(), ^{
//update your UI here
Person *thePerson = (Person *)[[delegate managedObjectContext] objectWithID:objectID];
self.myUIElement.text = person.firstname;