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:
- Do I need to use the
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? - Do I need to setup the
container.viewContext
parent to some private context created fromcontainer.newBackgroundContext()
? - What is the best approach to create a background task that is updating some data in the background thread on not main queue and inform about this main
viewContext
? - What is the best way to inform background context that about changes in model that user introduced in main
viewContext
in UI? - Do you know some books that covers those topics including architecture and concurrency changes in iOS 10?
NSPersistentContainer
has a methodperformBackgroundTask()
: 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 anNSFetchResultsController
which has a lot of delegate methods that help you respond to changes.NSPersistentContainer
and also the What's New in Core Data page.