Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSManagedObjectContext confusion

I am learning about CoreData. Obviously, one of the main classes you entouer is NSManagedObjectContext. I am unclear about the exact role of this. From the articles i've read, it seems that you can have multiple NSManagedObjectContexts. Does this mean that NSManagedObjectContext is basically a copy of the backend?

How would this resolve into a consistent backend when there is multiple different copies lying around?

So, 2 questions basically:

Is NSManagedContext a copy of the backend database?

and...

For example, say I make a change in context A and make some other change in context B. Then I call save on A first, then B? will B prevail?

Thanks

like image 453
0xSina Avatar asked Jan 09 '12 15:01

0xSina


People also ask

What is the purpose of NSManagedObjectContext?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

What is NSManagedObjectContext in Swift?

An object space to manipulate and track changes to managed objects.


1 Answers

The NSManagedObjectContext is not a copy of the backend database. The documentation describes it as a scratch pad

An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application. Its primary responsibility is to manage a collection of managed objects. These objects form a group of related model objects that represent an internally consistent view of one or more persistent stores. A single managed object instance exists in one and only one context, but multiple copies of an object can exist in different contexts. Thus object uniquing is scoped to a particular context.

The NSManagedObjectContext is just a temporary place to make changes to your managed objects in a transactional way. When you make changes to objects in a context it does not effect the backend database until and if you save the context, and as you know you can have multiple context that you can make changes to which is really important for concurrency.

For question number 2, the answer for who prevails will depend on the merge policy you set for your context and which one is called last which would be B. Here are the merge policies that can be set that will effect the second context to be saved.

NSErrorMergePolicyType
Specifies a policy that causes a save to fail if there are any merge conflicts.

NSMergeByPropertyStoreTrumpMergePolicyType
Specifies a policy that merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to external changes.

NSMergeByPropertyObjectTrumpMergePolicyType
Specifies a policy that merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to in-memory changes.

NSOverwriteMergePolicyType
Specifies a policy that overwrites state in the persistent store for the changed objects in conflict.

NSRollbackMergePolicyType
Specifies a policy that discards in-memory state changes for objects in conflict.

like image 175
Joe Avatar answered Oct 17 '22 05:10

Joe