1

How the multiple ManagedObjectContext (MOC) works in core data(Swift 2, iOS 9). I have been through lots of links & material online and answers on StackOverflow, but couldn't find exact answer.

I want to know, suppose I have created main MOC which points to PersistentStoreCoordinator (PSC)and another private queue MOC which has parent context set to above mentioned main MOC.

Question 1: Then How this works ? Is the hierarchy built like this : Private queue MOC --> Main queue MOC --> PSC.

Question 2: If I call save on 'private queue MOC', will it save to main MOC and in turn automatically main MOC will save to PSC ? or After save on Private MOC we have to call explicit save on Main MOC to save it to PSC ?

I just started working on core data and online links are not so clear. So, any simplified explanation will be much appreciated.

Thanks!

Tejas
  • 1,050
  • 12
  • 23

1 Answers1

1

For question 1, the hierarchy is however you create it. If you create a context with no parent, and then another one whose parent context is the first context, it'll be as you describe.

For question 2 I'll turn to the documentation on NSManagedObjectContext:

When you save changes in a context, the changes are only committed “one store up.” If you save a child context, changes are pushed to its parent. Changes are not saved to the persistent store until the root context is saved.

Saving is never automatic, so the parent context doesn't save changes until you tell it to save.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170