简体   繁体   English

automaticMergesChangesFromParent不执行任何操作

[英]automaticallyMergesChangesFromParent doesn't do anything

I was watching a WWDC 2016 (What's new in Core Data) video about some "new" changes and at some point it's said that this new property automaticallyMergesChangesFromParent is supposed to automatically merge the changes from the parent on the child context. 我正在观看有关某些“新”更改的WWDC 2016(Core Data中的新增功能)视频,在某些时候,有人说此新属性automaticallyMergesChangesFromParent应该自动合并子级上下文中父级的更改。

I created a simple test case: 我创建了一个简单的测试用例:

CustomObject *customObject = [[CustomObject alloc] initWithContext:self.persistentContainer.viewContext];
customObject.name = @"TEST";
customObject.customID = 2252;
self.persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;

NSManagedObjectContext *firstContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
firstContext.parentContext = self.persistentContainer.viewContext;
firstContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
firstContext.automaticallyMergesChangesFromParent = YES;

CustomObject *contextObj = [firstContext objectWithID:customObject.objectID];
NSLog(@"NAME [%@]", contextObj.name);
customObject.name = @"JO";
[self.persistentContainer.viewContext save:NULL];
NSLog(@"NAME [%@]", contextObj.name);

The output is: 输出为:

 NAME [TEST]
 NAME [TEST]

I was hoping it would be: 我希望是这样的:

NAME [TEST]
NAME [JO]

if I use a [firstContext refreshAllObjects]; 如果我使用[firstContext refreshAllObjects]; it will work as I expect, but it makes not difference if automaticallyMergesChangesFromParent is set to YES or NO. 它会按我预期的那样工作,但是如果将automaticallyMergesChangesFromParent设置为YES或NO,则没有区别。

Am I missing something on how this is supposed to work? 我是否想知道应该如何工作? The documentation doesn't help much. 文档没有太大帮助。

Thanks. 谢谢。

You are fetching and updating object in firstContext. 您正在firstContext中获取和更新对象。 To verify automaticallyMergesChangesFromParent , you should fetch it from self.persistentContainer.viewContext and update. automaticallyMergesChangesFromParent验证self.persistentContainer.viewContext ,您应该从self.persistentContainer.viewContext获取并更新。

self.secondObject = [[SecondCustomObject alloc] initWithContext:self.persistentContainer.viewContext];
self.secondObject.name = @"TEST";
self.persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;



NSManagedObjectContext *firstContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
firstContext.parentContext = self.persistentContainer.viewContext;
firstContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
firstContext.automaticallyMergesChangesFromParent = YES;
//firstContext.stalenessInterval = 0;

SecondCustomObject *contextObj = [self.persistentContainer.viewContext objectWithID:self.secondObject.objectID];
NSLog(@"NAME [%@]", contextObj.name);
contextObj.name = @"JO";
[self.persistentContainer.viewContext save:NULL];
SecondCustomObject * contextObj1 = [firstContext objectWithID:self.secondObject.objectID];
NSLog(@"NAME [%@]", contextObj.name);
NSLog(@"NAME [%@]", contextObj1.name);


 NAME [TEST]
 NAME [JO]
 NAME [JO]

When you set automaticallyMergesChangesFromParent = YES; automaticallyMergesChangesFromParent = YES;设置时, automaticallyMergesChangesFromParent = YES; and then make save in your parent context CoreData automatically calls mergeChangesFromContextDidSaveNotification: on your child context by dispatching this block on the child queue (by calling performBlock: as usual). 然后在父上下文中进行保存,CoreData会通过在子队列上分派此块(通过照常调用performBlock: :)来自动在子上下文上调用mergeChangesFromContextDidSaveNotification: As soon as both your contexts work on the main queue you'll see changes only on the next main loop 当两个上下文都在主队列上工作时,您只会在下一个主循环上看到更改

Try 尝试

CustomObject *contextObj = [firstContext objectWithID:customObject.objectID];
NSLog(@"NAME [%@]", contextObj.name);
customObject.name = @"JO";
[self.persistentContainer.viewContext save:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
   NSLog(@"NAME [%@]", contextObj.name);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM