Last active
August 29, 2015 14:10
-
-
Save nvkiet/a28bbc22488a324df61d to your computer and use it in GitHub Desktop.
multi-context-coredata
This file contains hidden or 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
| 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