Currently, [May 2015], Apple Concurrency with documentation on master data is at best very misleading, as it does not cover any improvements in iOS 5 and therefore no longer shows the best ways to use the kernel data at the same time. There are two very important changes in iOS 5: the parent context and the new types of concurrency / threading.
I have not yet found written documentation that comprehensively covers these new features, but WWDC 2012 video βSession 214 - Key recommendations on the best dataβ explains all this very well.
Magical Record takes advantage of these new features and may be worth the look.
The real basics are the same: you can still use only managed objects into which a stream was created, controlled by the object context.
Now you can use [moc performBlock:] to run the code in the desired thread.
No need to use mergeChangesFromContextDidSaveNotification: more; instead, create a child context for changes, and then save the child context. Saving the child context will automatically introduce the changes into the parent context, and to save the changes to disk, simply save to the parent context in it.
To do this, you must create a parent context with a parallel type, for example:
mainManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
Then in the background thread:
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType]; [context setParentContext:mainManagedObjectContext]; <... perform actions on context ...> NSError *error; if (![context save:&error]) { <... handle error ...> } [mainManagedObjectContext performBlock:^{ NSError *e = nil; if (![mainContext save:&e]) { <... handle error ...> } }];
JosephH Aug 05 '12 at 8:21 2012-08-05 08:21
source share