Search code examples
iphoneiosios4core-dataentity-relationship

How to save individual entities in Core data


I have 4 entities in core data. Let's say entities A, B, C and D. These are how the relationships are:

A <----->> B

B <<-----> C

B <<---->> D

The problem is: I have lots of screens in my application. Now, suppose, in first screen, I make changes to entity A, and currently i don't call save on the managed object context, (I have only one managed object context). Now i make a change to entity B, and now i want to save the changes made to entity B to the persistent store, but do not want the changes i have made to A to be saved till now. It is a requirement in my app like this. So i want to save entity A later, or probably not save it at all.

In short, the question is, how to save entities individually to the persistent store? I have thought a lot about this but not been able to find out a way to do this. I tried using NSUndoManager that core data provides, but it made things really complicated.

Any thoughts on how to do it?


Solution

  • Have each viewContoller use it's own a separate local managed object context. Then set the property parentContext (property of NSManagedObjectContext) accordingly, either to the to the main context (the one with the persistent store), or another context in the viewController Hierarchy. Look up the WWDC2011 iOS5 Core Data video, which talks about parent/child managedObjectContexts.

    Sounds like a jumble though and you might want to aim for a redesign.