Skip to content

Instantly share code, notes, and snippets.

@nvkiet
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save nvkiet/a28bbc22488a324df61d to your computer and use it in GitHub Desktop.

Select an option

Save nvkiet/a28bbc22488a324df61d to your computer and use it in GitHub Desktop.
multi-context-coredata
Solution 1:
NSMangedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.parentContext = mainMOC;
[temporaryContext performBlock:^{
// do something that takes some time asynchronously using the temp context
// push to parent
NSError *error;
if (![temporaryContext save:&error])
{
// handle error
}
// save parent to disk asynchronously
[mainMOC performBlock:^{
NSError *error;
if (![mainMOC save:&error])
{
// handle error
}
}];
}];
Solution 2:
NSMangedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.persistentStoreCoordinator = mainMOC.persistentStoreCoordinator;
[temporaryContext performBlock:^{
// do something that takes some time asynchronously using the temp context
// push to parent
NSError *error;
if (![temporaryContext save:&error])
{
// handle error
}
}];
}];
Child context
func didFinishViewController(
viewController:JournalEntryViewController, didSave:Bool) {
// 1
if didSave {
// 2
var error: NSError? = nil
let context = viewController.context
context.performBlock({ () -> Void in
if context.hasChanges && !context.save(&error) {
println(
"Couldn't save: \(error), \(error?.userInfo)")
abort()
}
// 3
self.coreDataStack.saveContext()
})
}
// 4
dismissViewControllerAnimated(true, completion: {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment