我已经设法简化了编码以重现错误,并希望与其他人一起尝试这一点的第二意见.
这是一个完整性检查,因为我怀疑iOS 7引入的核心数据问题,因为这在iOS 6中运行正常.
首先,启动一个新的核心数据项目并创建这个模型……
属性’current’是可选的布尔值.一对多关系是有序关系,创建NSOrderedDataset.
现在为应用添加几个按钮;第一个创建数据(一个项目和两个相关的’图纸’),第二个交换两个图纸,然后在第一个图纸中设置一个属性.
这是代码……
-(IBAction)onTestButton:(id)sender { id delegate = [[UIApplication sharedApplication]delegate]; NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext]; self.project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:managedObjectContext]; Drawing *drawing1 = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:managedObjectContext]; Drawing *drawing2 = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:managedObjectContext]; NSMutableOrderedSet* tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.project.drawings]; [tempSet addObject:drawing1]; [tempSet addObject:drawing2]; self.project.drawings = tempSet; [self save]; } -(IBAction)onTestButton2:(id)sender { NSMutableOrderedSet *exchange = [self.project mutableOrderedSetValueForKey:@"drawings"]; [exchange exchangeObjectAtIndex:0 withObjectAtIndex:1]; self.project.drawings = exchange; [self save]; Drawing *drawing = [self.project.drawings objectAtIndex:0]; BOOL current = [drawing.current boolValue]; drawing.current = [NSNumber numberWithBool:!current]; [self save]; } -(void)save { id delegate = [[UIApplication sharedApplication]delegate]; NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext]; NSError *error = nil; if( ![managedObjectContext save:&error] ) { NSLog(@"%@ Save: Unresolved Error on Save %@",error,[error userInfo] ); abort(); } }
现在,按第一个测试按钮进行测试.这会设置数据.
然后按第二个测试按钮……一切OK!
现在再次按下第二个测试按钮并弹出.您应该得到类似于此的NSMergeConflict错误….
Save: Unresolved Error on Save {
conflictList = (
“NSMergeConflict (0x8a7d0b0) for NSManagedObject (0x8bedfa0) with objectID ‘0x8bd9340
‘ with
oldVersion = 1 and newVersion = 2 and old object snapshot = {\n
current = \”\”;\n project = \”0x8bc3f50
\”;\n}
and new cached row = {\n current = \”\”;\n project =
\”0x8aa88c0
\”;\n}”
);
我从错误中注意到我的Project对象已经改变.但是,这是使用主应用程序上下文的单线程应用程序.
我花了太长时间在这上面,并希望其他人就问题所在地发表评论.这是一个核心数据错误,还是我是一个正确的’傻瓜’?
非常感谢
/适合
解决方法
http://prod.lists.apple.com/archives/cocoa-dev/2013/Oct/msg00657.html
简而言之 – 将其添加到您的MOC设置中:
[_managedObjectContext setMergePolicy:[[NSMergePolicy alloc] initWithMergeType:NSMergeByPropertyObjectTrumpMergePolicyType]];
当使用NSsqlStoreType时,这也会影响OS X 10.9 Mavericks.我无法使用NSXMLStoreType重复合并错误.
对于Swift 2.x
managedObjectContext.mergePolicy = NSMergePolicy(mergeType: .MergeByPropertyObjectTrumpMergePolicyType)