0

好吧,我正在测试我的应用程序的最后阶段,现在我遇到了一个难倒我的问题。iCloud合并后,如何解决“CoreData:错误:严重的应用程序错误”?

的过程如下:

1)经由coredata切换到链接到myTableView在firstViewController

2)作为上述代码的一部分“主表”我也删除所有先前对象并将字符串保存到“同步日志表”中。结果该表应该留下一个对象,例如一个字符串“星期一20:33”。 注意:这是一个新实现的功能,我相信它是问题的根源,因为我使用它来在所有设备上填充标签以显示上次同步的数据,换句话说,用户一个简单的检查,所有设备都具有相同的信息,并及时更新

3)两者的变化,然后通过调用[managedObjectContext save:&error];

4)在第二个设备都保存的非常好了,我可以做更新到'主表',在调用代码重新加载后显示在myTableView上。

注意我在第二台设备上显示了myTableView中的3行,并且由于谓词是过滤结果,因此第1步中对“Main Table”所做的更改不会更改第二个设备上的tableview。

那么问题开始

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委托方法,它应该只接收来自“主表”的对象,从而抛出计数并导致错误。

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

回答

0

您的tableview对象(即获取控制器或NSArray,无论)需要更新,然后在tableview本身调用insertRowsAt…deleteRowsAt…。 tableview告诉你这些数字不会加上它所期望的 - 如果你正在使用一个获取控制器,也许在你调用[moc mergeChangesFromContextDidSaveNotification:note]之后,你需要发送一个保存消息给moc,或者强制刷新你的表格视图的数据对象。

相关问题