NSManagedObjectContext Confusion

I am learning CoreData. Obviously, one of the main classes you joined is NSManagedObjectContext. I don’t understand exactly what role this plays. From the read articles, it seems that you can have several NSManagedObjectContexts. Does this mean that NSManagedObjectContext is basically a copy of the backend?

How will this be resolved in a sequential backend if there are several different copies?

So 2 questions basically:

Is NSManagedContext a copy of the backend database?

and...

For example, let's say I make changes in context A and make some other changes in context B. Then I call save on A first and then B? will B prevail?

thanks

+7
source share
2 answers

NSManagedObjectContext not a copy of the database. The documentation describes it as a notepad.

An instance of NSManagedObjectContext represents a single "space object" or "scratch" in the application. to manage a collection of managed objects. These objects form a group of related model objects, which are an internally consistent representation of one or more persistent stores. One instance of a managed entity exists in one and only one context, but multiple copies of an entity may exist in different contexts. In this way, object binding is tied to a specific context.

NSManagedObjectContext is just a temporary place to make changes to managed objects in a transactional way. When you make changes to objects in a context, it does not affect the backend database as long as you save the context, and, as you know, you can have several contexts that you can make changes that are really important for concurrency .

For question number 2, the answer for who prevails will depend on the merge policy that you set for your context and which is called the last one which will be equal to B. Below are the merge policies that can be set, which will affect the preservation of the second context.

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

NSMergeByPropertyStoreTrumpMergePolicyType
Defines a policy that combines conflicts between the persistent version of the object store and the current version in memory, giving priority to external changes.

NSMergeByPropertyObjectTrumpMergePolicyType
Defines a policy that combines conflicts between the persistent storage version of an object and the current version in memory, giving priority in memory.

NSOverwriteMergePolicyType
Defines a policy that overwrites persistent storage for changed objects in conflict.

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

+11
source

NSManagedObjectContext is a concrete representation of your data model. Each context maintains its own state (for example, context), so changes in one context will not directly affect other contexts. When you work with multiple contexts, you are responsible for reconciling them by merging changes when the context saves its changes to the repository.

Your question is about this process and may also include merge conflicts. Whenever you save a context, its changes are committed to the repository, and the merge policy is used to resolve conflicts.

When you save the context, it will publish various progress notifications. In your case, if [contextA save:&error] succeeds, the context will post an NSManagedObjectContextDidSaveNotification notification. When you have multiple contexts, you usually observe this notification and call:

 [contextB mergeChangesFromContextDidSaveNotification:notification]; 

This will merge the changes stored on contextA into contextB .

EDIT: deleted the comment "thread safe". NSManagedObjectContext not thread safe.

+2
source

All Articles