0

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
TouchMint
  • 111
  • 3
  • 9

2 Answers2

6

You have to create a new object in the managed object context and then copy all attributes and relationships:

Question *question2 = [NSEntityDescription
                           insertNewObjectForEntityForName:@"Question"
                           inManagedObjectContext:context];
// Copy attributes:
question2.attr1 = question1.attr1;
question2.attr2 = question1.attr2;
// ...
// Copy relationships:
question2.rel1 = question1.rel1;
// ...

This can be automated using the NSEntityDescription of question1 and 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.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks for the reply but I think you misunderstood me which is understandable because my question is kind of confusing. I edited my initial question above to give some more information. – TouchMint Dec 01 '12 at 09:21
  • 1
    @TouchMint: I am sorry, but that is how I understood your question. - I cannot see an updated question yet, but if you provide more information, I will try to help. – Martin R Dec 01 '12 at 09:32
1

Martin's answer is the correct one. You can only instantiate an NSManagedObject subclass within a managed object context.

In your case, you say you don't want to persist the copied object, you just want to use it. In which case there are options:

  1. Create the new object within the current managed object context but don't save the objects .
  2. Create a new managed object context create your copies within this managed object context and don't save the contents of this second moc when deleting it.
  3. Create a custom class that isn't a subclass of NSManagedObject. This class just has a copy of the fields from your Question class that you want to use. Write an initialiser for this new class that takes a Question object. Then you can use this new object within your loop and not worry about persisting it.
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Ok I will give those solutions a shot. I guess I was really hoping I wouldnt have to pull in the managed object context. currently I just passed the one quiz item. I just find it weird that it cant just create a duplicate like a string or int. I really just needed it as like a variable. Anyways I will check him right and upvote you thanks for the help. – TouchMint Dec 01 '12 at 09:53