开发者

Getting NSManagedObject from another NSManagedObjectContext (referring the same NSPersistentStoreCoordinator)

开发者 https://www.devze.com 2022-12-16 01:39 出处:网络
i have got two different NSManagedObjectContexts both referring to the same NSPersistentStoreCoordinator, say context1 and context2.

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

I have an NSManag开发者_开发问答edObject 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?


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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消