问题
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:
- Can I search
objectA
in main context, remove it from there, and useobjectWithID
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. - Instead, should I remove
objectA
from main context, then save private context, and then fetch againobjectA
from main context? - 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