如何解决由于 iCloud 合并而导致的“CoreData:错误:严重的应用程序错误”?

Posted

技术标签:

【中文标题】如何解决由于 iCloud 合并而导致的“CoreData:错误:严重的应用程序错误”?【英文标题】:How can I resolve "CoreData: error: Serious application error" as a result of iCloud merge? 【发布时间】:2012-05-29 11:21:57 【问题描述】:

好的,我的应用程序测试已进入最后阶段,现在我遇到了一个困扰我的问题。

流程如下:

1) 通过 coredata 更改为“主表”,该表链接到 firstViewController 中的myTableView

2) 作为上述代码的一部分,我还将删除所有先前的对象并将字符串保存到“同步日志表”。因此,该表应该只剩下一个对象,例如一个字符串。 “周一 20:33”。 注意:这是一个新实现的功能,我认为它是问题的根源,我想要它,因为我正在使用它来填充所有设备上的标签以显示最后同步的数据,在其他让用户轻松检查所有设备是否具有相同信息并且是最新的单词

3) 然后通过调用[managedObjectContext save:&error]; 保存这两个更改

4) 在第二台设备上一切正常,在调用代码重新加载后,我可以更新显示在myTableView 上的“主表”。

请注意,我在第二台设备上的myTableView 中显示了 3 行,并且在步骤 1 中对“主表”所做的更改不会更改第二台设备上的表视图,因为谓词正在过滤结果。

那么问题就开始了

5) 第二台设备开始通过 iCloud 和委托方法接收更改:

- (void)mergeiCloudChanges:(NSNotification*)note forContext:(NSManagedObjectContext*)moc 
NSLog(@"AppDelegate Merge icloud changes for context");
 //***Specifically the line below!!
[moc mergeChangesFromContextDidSaveNotification:note]; 

NSNotification* refreshNotification = [NSNotification notificationWithName:@"RefreshAllViews" object:self  userInfo:[note userInfo]];

[[NSNotificationCenter defaultCenter] postNotification:refreshNotification]; 

上面代码中指示的行在我的 firstViewController 中启动了以下方法

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
NSLog(@"I'm inside the method Controller Will Change Context");
[self.myTableView beginUpdates];


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath 
NSLog(@"I'm inside the method Controller did Change Object");

UITableView *tableView = self.myTableView;

switch(type) 
        // And enters in this line here
    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                         withRowAnimation:UITableViewRowAnimationFade];
        break;
   

然后我得到错误:

CoreData: error: Serious application error.  
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  
Invalid update: invalid number of rows in section 0.  
The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)

收到此错误后,myTableView 不再响应,例如它不会更新或响应滑动删除。

根据我的理解,从“同步日志表”中合并的对象被插入(当它们不应该插入时)到 myTableView 委托方法中,该方法应该只接收来自“主表”的对象,从而抛出计数并导致错误。

知道如何在我的情况下解决这个问题吗?任何帮助将不胜感激

【问题讨论】:

【参考方案1】:

在你调用insertRowsAt…deleteRowsAt… 之前,需要更新你的tableview 对象(即,获取控制器或NSArray,等等)。表格视图告诉您,数字加起来不符合预期 - 如果您使用的是获取控制器,也许在您调用 [moc mergeChangesFromContextDidSaveNotification:note] 之后,您需要向 moc 发送保存消息,或强制刷新您的表格视图的数据对象。

【讨论】:

以上是关于如何解决由于 iCloud 合并而导致的“CoreData:错误:严重的应用程序错误”?的主要内容,如果未能解决你的问题,请参考以下文章

CoreData 同步 iCloud 数据导致 App 启动超时被系统 watchdog 终止的原因及解决

CoreData 同步 iCloud 数据导致 App 启动超时被系统 watchdog 终止的原因及解决

Ready iCloud + Core Data 解决方案

为啥我在 Gitlab 合并请求中收到“由于未验证用户而导致管道失败”和“分离的合并请求管道”?

iCloud Core 数据同步设置

如何解决包冲突问题