我有一个从context1 中NSManagedObject
获取的人 Scott 。NSManagedObjectContext
我也从NSManagedObjectContext
context2 中获取了相同的 Scott。
我知道如果我从同一个上下文中获取它们,如果一个被更新,另一个也将被更新,但我当前的结构不会轻易让我传递相同的上下文。我想知道是否还有另一种方式,类似于KVO。
当我对 context1 中的 Person Scott 进行更改时,我可以在 context2 中更新 Person Scott 吗?
编辑:
尝试测试:
NSError *error = nil;
NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
NSManagedObjectContext *context2 = [NSManagedObjectContext MR_context];
Boundary *boundary1 = [Boundary MR_findFirstInContext:context];
DLog(@"boundary1.name: %@", boundary1.name);
DLog(@"boundary1.managedObjectContext: %@", boundary1.managedObjectContext);
Boundary *boundary2 = (Boundary *)[context2 existingObjectWithID:boundary1.objectID error:&error];
DLog(@"boundary2.name: %@", boundary2.name);
DLog(@"boundary2.managedObjectContext: %@", boundary2.managedObjectContext);
boundary1.name = @"new name";
// Added this, does not change results
[context2 MR_observeContext:context];
DLog(@"boundary1.name: %@", boundary1.name);
DLog(@"boundary1.managedObjectContext: %@", boundary1.managedObjectContext);
DLog(@"boundary2.name: %@", boundary2.name);
DLog(@"boundary2.managedObjectContext: %@", boundary2.managedObjectContext);
结果:
DEBUG | -[LoginViewController viewDidLoad] | boundary1.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | boundary1.managedObjectContext: <NSManagedObjectContext: 0x1cdc6b20>
DEBUG | -[LoginViewController viewDidLoad] | boundary2.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | boundary2.managedObjectContext: <NSManagedObjectContext: 0x1cdd7930>
DEBUG | -[LoginViewController viewDidLoad] | boundary1.name: new name
DEBUG | -[LoginViewController viewDidLoad] | boundary1.managedObjectContext: <NSManagedObjectContext: 0x1cdc6b20>
DEBUG | -[LoginViewController viewDidLoad] | boundary2.name: 997677
DEBUG | -[LoginViewController viewDidLoad] | boundary2.managedObjectContext: <NSManagedObjectContext: 0x1cdd7930>
对 ' 名称的更改boundary1
不会保留在boundary2
' 名称中。