Strange parent / child NSManagedObjectContext phenomenon

本秂侑毒 提交于 2019-12-23 07:27:17

问题


I have created two context like this:

// create writer MOC
_privateWriterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_privateWriterContext setPersistentStoreCoordinator:_persistentStoreCoordinator];

// create main thread MOC
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_managedObjectContext.parentContext = _privateWriterContext;

I have a NSFetchResultedController initiated with _managedObjectContext.

I know it is strange, but I am adding a record to the parent to _privateWriterContext, I am saving it.

What is surprising is that child context and so FRC gets notified about this event. Why? I have not reset-ed child, or anything else. I thought they are independent entities as long as child context will not get saved.


In @pteofil article I found this line:

When a change is made in a context, but not saved, it is visible to all of its’ descendants but not to its’ ancestors.

.. it is pushed to the persistent store (via the persistent store coordinator) and becomes visible to all contexts connected to the store.


回答1:


This is not supposed to happen. Adding an NSManagedObject ('record' ) to the parentContext, will not make the child aware of this object automatically. Only when you make the childContext execute a fetch, then it will fetch from the parentContext. To figure out what is going on in your case, here are some suggestions:

  • figure out when a fetch is executed on the childContext (this is done by the fetchedRestultsController when you set it up. Check if that fetch happens before or after you add the managedObject to the parentContext).
  • set breakpoints in all four delegate callbacks of the fetchedResultsController to find out for which object it is calling the methods (and see if it is the object you just added to the parentContext).
  • make absolutely sure you know which context you are sending messages too.

I have a used a similar approach, but different: the childContext is the context is used to parse in new data (on a private queue), and when this parsing is done, the chid calls save:. This will save the changes up to the parent, which is in my case the mainQueueContext. This call to save: will cause the mainQueueContext to receive all the newly parsed objects, and any fetchedResultsController using that mainQueueContext will then call it's delegate methods for the new/changed/updated/delete objects. You could try inverting your child/parent relationship too and see if it works as described in the docs, just to find out whats going on.




回答2:


I strongly recommend avoiding parent-child context setups. Our book goes into detail why they often lead to strange behaviour: https://www.objc.io/books/core-data/

Short story: They're not as independent as you might think.

Use multiple context that share a single persistent store coordinator if you need more than a single context.



来源:https://stackoverflow.com/questions/30016148/strange-parent-child-nsmanagedobjectcontext-phenomenon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!