IOS 10+ Persistence Store Container set parent context to main context

480 Views Asked by At

Following a perfect core data stack, we follow this hierarchy.

enter image description here

For IOS 10+, Apple introduced NSPersistentContainer. In which Apple described the way for getting background context by

let backgroundcontext = persistentContainer.newBackgroundContext()

And the Main Context is described as

let maincontext = persistentContainer.viewContext

And NSPersistentContainer is associated with this backgroundcontext. So any save operation on this context will be passed on to NSPersistentStoreCoordinator.

My Question is how to follow this hierarchy mentioned in the image? No tutorial has mentioned changing the parent context for maincontext.

Means saving in backgroundcontext and maincontext will do I/O operation to NSPersistentStoreCoordinator?

The perfect CoreData Stack should be like that

Saving in maincontext will get saved in backgroundcontext. And Save operation on backgroundcontext will save data to NSPersistentStoreCoordinator.

How this should be achieved?

0

There are 0 best solutions below