开发者

Making a copy of a NSManagedObject which isn't managed

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

Now, I want to make a copy of开发者_高级运维 'b', so that 'b' can be modified, but not saved to the store, but 'A' can be saved'.

For example,

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 that this isn't particularly clear, but I just want to make a temporary copy of a managed object that I can edit, but not persist the changes even though I'm going to call 'save'.

Let me know if you have any questions, I know I might need to clarify this for you.


I see a couple solutions:

  1. Create another entity for the purpose of being temporary. It would have the same parent class as the object that it is copying, but it would not have all the relationships. Insert it into the context and copy the values over. Keep an array of these temporary objects and delete them all from the context when appropriate. I have done this before and it worked, but it's a bit complicated.

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

Keep in mind how you are copying an object. You may want to make a deeper copy (creating a new object to hold an attribute) rather than point to the same attribute object as the original object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜