views:

200

answers:

1

Hi,

i have got two different NSManagedObjectContexts both referring to the same NSPersistentStoreCoordinator, say context1 and context2.

I have an NSManagedObject out of context1. As I would like to edit it, not knowing whether it will be saved afterwards, I would like to get that object from context2. Context2 could be just trashed in the case that I do not want to save the NSManagedObject. In case of a save, I will merge context2 in context1.

But how do I get the object from context2? Is there an easy way to do this, or do I have to request the object with an predicate xyz=[NSManagedObject objectId]? And what does xyz have to be in that case?

A: 

You can specify the target NSManagedObjectContext, i.e.

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];
    myArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

Addition to your comment:

context2object = [myEntityArrayFromContext2 objectAtIndex:[myEntityArrayFromContext1 indexOfObject:context1object]];
Henrik P. Hessel
Right. That's what I did to get the managed object from context1. But now the user decides to edit that object. That is the reason why i need that same object from context2. So how do i get that? Through the objectID?
Shingoo
through entityForName:
Henrik P. Hessel
I'm not shure whether you know what I meant ;)I got all objects using your code above.It returned me an array of 5 Objects of type "MyEntity".Clear so far.Now I take for example the third element out of that array. This element is of course an Object of class "MyEntity".Now my question I tried to ask obove:How do I get that third Object of class type "MyEntity" out of contexts2? Or more general: How do I associate an existing NSManagedObject to another context?
Shingoo
see my edit. does it help?
Henrik P. Hessel
Too easy... Sorry for that silly question.... Thanks a lot.
Shingoo