Doubts About Core Data NSManagedObject Deep Copy

Posted by Jigzat on Stack Overflow See other posts from Stack Overflow or by Jigzat
Published on 2010-06-09T22:13:54Z Indexed on 2010/06/10 3:42 UTC
Read the original article Hit count: 450

Hello everyone, I have a situation where I must copy one NSManagedObject from the main context into an editing context. It sounds unnecessary to most people as I have seen in similar situations described in Stackoverflow but I looks like I need it.

In my app there are many views in a tab bar and every view handles different information that is related to the other views. I think I need multiple MOCs since the user may jump from tab to tab and leave unsaved changes in some tab but maybe it saves data in some other tab/view so if that happens the changes in the rest of the views are saved without user consent and in the worst case scenario makes the app crash.

For adding new information I got away by using an adding MOC and then merging changes in both MOCs but for editing is not that easy. I saw a similar situation here in Stackoverflow but the app crashes since my data model doesn't seem to use NSMutableSet for the relationships (I don't think I have a many-to-many relationship, just one-to-many) I think it can be modified so I can retrieve the relationships as if they were attributes

for (NSString *attr in relationships) {
    [cloned setValue:[source valueForKey:attr] forKey:attr];
}

but I don't know how to merge the changes of the cloned and original objects. I think I could just delete the object from the main context, then merge both contexts and save changes in the main context but I don't know if is the right way to do it. I'm also concerned about database integrity since I'm not sure that the inverse relationships will keep the same reference to the cloned object as if it were the original one.

Can some one please enlighten me about this?

© Stack Overflow or respective owner

Related posts about core-data

Related posts about object