NSMergeConflict with newVersion = <deleted>

时间:2016-11-22 08:01:09

标签: ios objective-c nsmanagedobject nsmanagedobjectcontext

在花了很多时间处理这个错误后,我来这里询问是否有人有关于此错误的信息。我加载了两个实体,一个Parks实体和一个GuidedTour实体。 Paks可以与许多GuidedTours相关,但当我尝试保存它时,错误会引发:

  

错误域= NSCocoaErrorDomain代码= 133020&#34;(null)&#34;的UserInfo = {conflictList =(       &#34; NSMergeConflict(0x17026afc0)for NSManagedObject(0x1740d94b0)with objectID&#39; 0xd000000000240002&#39; with oldVersion = 0和newVersion = and old cached row = {\ n language = \&#34; de_DE \&#34 ;; \ n text = \&#34; Apapapapapa&#34; ...}

我无法理解错误,我的合并政策是:NSMergeByPropertyStoreTrumpMergePolicyType

我的代码:

AppDelegate appDelegate =(AppDelegate )[[UIApplication sharedApplication] delegate];     self.context = [appDelegate managedObjectContext];

NSError *error = nil;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuidedTours" inManagedObjectContext:self.context ];
[fetchRequest setEntity:entity];

NSArray *fetchedObjects = [self.context  executeFetchRequest:fetchRequest error:&error];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"guided_tours_parks == NULL"];
fetchedObjects = [fetchedObjects filteredArrayUsingPredicate:predicate];

GuidedTours *tour = [fetchedObjects firstObject];



fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entityPark = [NSEntityDescription entityForName:@"Parks" inManagedObjectContext:self.context ];
[fetchRequest setEntity:entityPark];

fetchedObjects = [self.context  executeFetchRequest:fetchRequest error:&error];

predicate = [NSPredicate predicateWithFormat:@"name == %@",[jsonData objectForKey:@"Park ID"] ];
fetchedObjects = [fetchedObjects filteredArrayUsingPredicate:predicate];

Parks *park = [fetchedObjects firstObject];


[tour setGuided_tours_parks:park];

[park addParks_guided_toursObject:tour];



// Save the object to persistent store
if (![self.context  save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}

有人有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我也有同样的错误,在我的情况下,它是在尝试执行NSBatchDeleteRequest之后在插入项上发生的。问题是执行NSBatchDeleteRequest之后,我没有调用[self.context reset]方法。

根据WWDC 2015演讲,批量更新/删除会绕过上下文并直接修改持久性存储文件。因此,如果您不调用reset,则您的上下文将包含已删除的对象,这些对象可能导致您遇到上述合并冲突。

https://developer.apple.com/videos/play/wwdc2015/220/