Can Managed Object Contexts in different threads (

2019-06-05 01:45发布

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

0条回答
登录 后发表回答