Core Data: should I be fetching objects from the parent context or does the child context have the same objects as the parent?

痴心易碎 提交于 2019-12-04 07:54:05

It does not seem to make too much sense to create a child context and then fetch from the parent context. I do not believe that this is the way child contexts' blocks were conceived to be used.

To clear up the confusion: after creating the child context from a parent context, that child context has the same "state" as the parent. Only if the two contexts do different things (create, modify, delete objects) the content of the two contexts will diverge.

So for your setup, proceed as follows:

  • create the child context
  • do the work you want to do (modifying or creating objects from the downloaded data),
  • save the child context

At this stage, nothing is saved to the persistent store yet. With the child save, the changes are just "pushed up" to the parent context. You can now

  • save the parent context

to write the new data to the persistent store. Then

  • update your UI,

best via notifications (e.g. the NSManagedObjectContextDidSaveNotification).

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