I have what I suppose is a pretty standard setup, with one MOC record that is never saved (contains a bunch of objects downloaded from the Internet) and another persistent MOC that stores the objects. When a user selects an object from scratchMOC to add to their library, I want to either 1) remove the object from scratchMOC and paste it into PermanentMOC, or 2) copy the object to PermanentMOC. The master data FAQ says that I can copy an object as follows:
NSManagedObjectID *objectID = [managedObject objectID]; NSManagedObject *copy = [context2 objectWithID:objectID];
(In this case, context2 will be a constant MOC.) However, when I do this, the copied object fails; data is not initially resolved. When it is resolved, later all values are zero; None of the data (attributes or relationships) from the original managed entity is copied or referenced. Therefore, I see no difference between using this objectWithID method and simply inserts a brand new object into PermanentMOC using insertNewObjectForEntityForName :.
I understand that I can create a new object in PermanentMOC and manually copy each pair of key values from the old object, but I'm not very happy with this solution. (I have several different managed objects for which I have this problem, so I don’t want to write and update copy methods: for all of them as development continues.) Is there a better way?
cocoa-touch cocoa core-data nsmanagedobject nsmanagedobjectcontext
Aeonaut Jun 08 2018-10-06T10: 00
source share