Can Managed Object Contexts in different threads (main/private queues) handle the same objects?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 01:51:24

问题


I have an NSManagedObjectContext in the main queue (the default context provided in AppDelegate), and I create another NSManagedObjectContext in a private queue to request data updates to web services. I use the main context to fetch all the objects to be shown and managed throughout the app, and I use the private context to insert the new objects I receive from services to avoid blocking the UI and to also avoid "interfering" with the objects in the main context in case the user and/or the app are operating with them. Both contexts are "siblings", they are not parent and child. This is because I need to create all new objects to later know if some of them should be removed (a property of the objects indicates that).

The point is, I then have duplicated sets of objects, one in the main context and another in the private context. They should have different object IDs, but according to the logic of my app they are the "same" objects. If I have an objectA in the main context, and I receive that same objectA in the private context but with updated values, I need to replace objectA in main context with objectA in private context. But they theoretically have different objectID. My questions are:

  1. Can I search objectA in main context, remove it from there, and use objectWithID to "transfer" objectA from private context to the main ? Having into account that main context is in main queue and the private is in a private queue.
  2. Instead, should I remove objectA from main context, then save private context, and then fetch again objectA from main context?
  3. Maybe I should handle this scenario in another way...

Thanks in advance

来源:https://stackoverflow.com/questions/32541859/can-managed-object-contexts-in-different-threads-main-private-queues-handle-th

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