Reputation: 5366
I was looking last time for some books related to Core Data in iOS 10, especially covering subjects like Query Generations, NSPersistentContainer, how to setup my Contexts hierarchy in new API with their new concurrency model, and so on.
I read that now every context should have their parents set to nil
, and the best way is to connect every Context directly to the Persistent Story Coordinator. There is no need to setup parent/child relationships between context if our goal is to responsive UI in our application. I have questions such as:
container.newBackgroundContext()
method to create context each time I need some task to perform in background or should I create one context and store it somewhere and reuse it for better performance? How many context I can create? What is the best number of background contexts?container.viewContext
parent to some private context created from container.newBackgroundContext()
?viewContext
?viewContext
in UI?Upvotes: 2
Views: 841
Reputation: 4480
If you set automaticallyMergesChangesFromParent
to true on any context, it will merge changes from any parent context. In iOS 10, any contexts created by the NSPersistentContainer
have the container's NSPersistentStoreCoordinator
as a parent. The effect is that if you set this to true on your viewContext
, changes saved on any newBackgroundContext
will automatically merge and vice versa.
Upvotes: 2
Reputation: 150565
NSPersistentContainer
has a method performBackgroundTask()
: just pass a block to this and it will create a new private context for you to run this block.NSPersistentContainer
deals with that for you.viewContext
for changes, then as long as you do a save in any of your background queues, this change will percolate up. Alternatively, use an NSFetchResultsController
which has a lot of delegate methods that help you respond to changes.NSPersistentContainer
and also the What's New in Core Data page.Upvotes: 4