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,或者強制刷新你的表格視圖的數據對象。

相關問題