Does a managed object in a parent context modify to a child context when doing a child fetch in Core Data?

I have set 2 NSManagedContext in the context of the parent parent in Core Data.

    _mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_mainContext setParentContext:_parentContext];
    [_mainContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

    _importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_importContext setParentContext:_mainContext];
    [_importContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

      

What happens if I did the following in order ("fetch" means NSFetchRequest):

  • Retrieve managed object A in mainContext.
  • Extract managed object A into importContext.
  • Update object A in mainContext.
  • Extract managed object A into importContext.

importContext (child) -> mainContext (parent) -> store save coordinator

My question is, in step 4, will the object that was selected in importContext updated with the changes I made in step 3 in the mainContext? Reading articles on the web seems to indicate that changes made to parentContext are not propagated to the child context if the child context has already loaded the data (because contexts are cached?)

+3


source to share


2 answers


Changes to the child context propagate to the parent when the child is saved, not when checked out. The changes do not move in the other direction.

In your scenario ...

Retrieve managed object A in mainContext.

mainContext will execute a fetch request against it NSPersistentStoreCoordinator

. The coordinator will find the repository that should serve the request and provide it with a sample. Persistent storage will return the result and depending on storage will keep a copy of the string data in memory. mainContext will now have a copy of this object in memory (but not necessarily any of its property values).

Extract managed object A into importContext.

importContext doesn't have a reference to this object in memory, so it will pass the parent mainContext to it . mainContext will return a reference to the object in memory A. If mainContext has not previously retrieved the object or turned into an error, mainContext will be fully loaded into persistent storage. Extract and objectWithID:

install as many levels as they need.



Update object A in mainContext.

Changed object A in memory A in mainContext .

Extract managed object A into importContext.

An object A in memory is returned in importContext . Changes made to mainContext will not be present in this object.

It is important to remember that when a context is created, this is a "snapshot" of the state of its parent. Subsequent changes to the parent will not be visible in the child if the child is somehow invalidated (i.e. invalidated or discarded).

You can see how the changes propagate in this animation .

+3


source


What do you mean by "bait"? The selected result controller?



From my point of view, the answer to your question is a general yes. Assuming some kind of shared context of the parent context, the two contexts will get a new object if one context saves with the changed object and the other context does something to load it again, for example, uses the objectID and - objectWithID:

on the other context to reload it ...

0


source







All Articles