title: Doubts About Core Data NSManagedObject Deep Copy
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <a href="https://stackoverflow.com/questions/2730832/how-can-i-duplicate-or-copy-a-core-data-managed-object/2936660#2936660">here</a> 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 </p>
  
  <pre><code>for (NSString *attr in relationships) {
      [cloned setValue:[source valueForKey:attr] forKey:attr];
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Can some one please enlighten me about this?</p>