Creating a copy of an unmanaged NSManagedObject

So my problem is that I have NSManagedObject 'A' that has a "-many" to "b" relationship. Therefore, for each object "A" there can be many "b".

Now I want to make a copy of "b", so that "b" can be changed, but not stored, but "A" can be saved. "

For instance,

self.title = A.name; setOfB = A.setOfb; // This is still managed by CoreData temporaryCopyOfB = [setOfB unManagedCopy];// I want to make a copy of b which isn't managed b = [temporaryCopyOfB objectAtIndex:0]; b.property = newValue; [A save]; //[setOfB objectAtIndex:0].property should still == oldValue 

I know this is not particularly clear, but I just want to make a temporary copy of the managed entity that I can edit, but I don’t save the changes, even if I call save.

Let me know if you have any questions, I know that I may need to clarify this to you.

+4
source share
1 answer

I see a couple of solutions:

  • Create another object with a temporary purpose. It will have the same parent class as the object that it copies, but it would not have all the relationships. Paste it into the context and copy the values. Keep an array of these temporary objects and remove them from the context when necessary. I have done this before and it worked, but it is a bit complicated.

  • Create a dictionary object and add to it all the keys and values ​​of the managed objects. This is probably easier, but I have not tried.

Keep in mind how you copy an object. You might want to make a deeper copy (creating a new object to hold the attribute) instead of pointing to the same attribute object as the original object.

+2
source

Source: https://habr.com/ru/post/1299843/


All Articles