I am not even sure if I am stating this right but I am trying to copy a user created coredata object intstance and having a pretty hard time figuring it out.
I have an object (Question) that is pulled in from the managedObjectContext.
This is from a Quiz in my database that has many Questions.
I have my Question instance in memory named question1. I want to copy question1 to an instance called question2.
Question *question2 = question1;
Works sometimes but crashes sometimes too (I know its not the right way to do it)
I have been messing with NSCopying and -(id)copyWithZone:(NSZone *) zone but I am just getting a blank instance back.
If anyone could help that would be great thanks!
Here is a little bit more detail.
I have question1 it is an instance of Question. for example question1.answer will return that questions answer.
I want to simply copy question1 to question2. I dont need to store question2 in the datamodel or anything I just need to use it in a loop then release.
Basically I want to do this:
Question *question2 = question1;
NSLog(@"%@", question2.answer)
The above is working sometimes but then sometimes I am getting random crashes on the nslog line.
You have to create a new object in the managed object context and then copy all attributes and relationships:
This can be automated using the
NSEntityDescriptionofquestion1and processing all attributes and relationships from the entity description, see the (great) answer to this question: How can I duplicate, or copy a Core Data Managed Object?. But note that that code duplicates also all related objects, which might not be what you want.I think that if there are not too many attributes/relationships, the “manual” copying is easier, and you can decide for each related object if that must also be copied or not.