我的應用程序有兩個標籤欄...每個用戶到一個tableviewcontroller,向他顯示一個項目列表。第一個視圖讓用戶記錄數據庫中的條目。另一個選項卡/視圖從數據庫讀取並將這些項目呈現給用戶,但是,不會從第二個視圖對coreData/persistant存儲進行更新。CoreData錯誤讓我瘋狂... CoreData:嚴重的應用程序錯誤。從NSFetchedResultsController委託捕獲的異常
當我通過第一個視圖控制器添加一個新項目時,它在視圖中完美顯示。但是,只要我點擊另一個標籤欄來查看新項目出現在該視圖控制器中,我會得到下面列出的錯誤,並且新添加的項目不會出現...注意:如果我停止應用程序並重新加載/重新運行它,並開始通過點擊第二個標籤欄,新項目顯示正常,所以我知道模型正在更新罰款。
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
2011-10-20 20:56:15.117 Gtrac[72773:fb03] 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 (4) 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 (0 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)
來自代理應用程序的代碼,其中managedObjectContext被傳遞給兩個viewControllers。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// get a point to the master database context
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
// Handle the error.
}
// create tab bar controller and array to hold each of the tab-based view controllers that will appear as icons at bottom of screen
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:5];
//
// setup first tab bar item
//
//
// alloc the main view controller - the one that will be the first one shown in the navigation control
RootViewController *rootViewController = [[RootViewController alloc] initWithTabBar];
// Pass the managed object context to the view controller.
rootViewController.managedObjectContext = context;
// create the navigation control and stuff the rootcontroller inside it
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
// set the master navigation control
self.navigationController = aNavigationController;
// add the navigaton controller as the first tab for the tab bar
[localControllersArray addObject:aNavigationController];
[rootViewController release];
[aNavigationController release];
//
// setup the other tab bar
//
//
// alloc the view controller
vcSimulator *vcSimulatorController = [[vcSimulator alloc] initWithTabBar];
UINavigationController *blocalNavigationController = [[UINavigationController alloc] initWithRootViewController:vcSimulatorController];
// Pass the managed object context to the view controller.
vcSimulatorController.managedObjectContext = context;
// add this controller to the array of controllers we are building
[localControllersArray addObject:blocalNavigationController];
// release these guys, they are safely stored in the array - kill these extra references
[blocalNavigationController release];
[vcSimulatorController release];
//
//
// ok, all the tab bars are in the array - get crackin
//
//
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
[window addSubview:[tabBarController view]];
[window makeKeyAndVisible];
return YES;
When I add a new item via the first viewcontroller, it shows up perfectly in the view. However, as soon as I tap on the other tab bar to see the new item appear in that viewcontroller, I get the error listed above, and the newly added item does not appear... Note: if I stop the app and reload/re-run it, and start by tapping the 2nd tabbar, the new item shows up fine, so I know the model is being updated fine.
Here are the tableview delegate methods from the 2nd view controller.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
任何幫助,您可以提供將是非常讚賞。
我搜查了這個網站,發現這個錯誤的很多例子,但沒有一個似乎很合適。我也看到引用推斷,這個錯誤我看到實際上是在蘋果代碼一個已知的bug ......
*更新的信息*
我已經回到並設置斷點在代碼,並用這些附加信息編輯原始問題。當用戶將新項目添加到數據庫時,他將從rootview轉換到listCourses視圖。添加事務完美地工作,listCoursesView UITableView更新完美。
當我點擊另一個視圖,它也從相同的核心數據模型中讀取數據時,它的viewcontroller按照以下順序運行,但永遠不會將新項添加到tableview中。這是它經歷的順序。
模擬器VC:
- controllerWillChangeContent which runs...
[self.tableView beginUpdates];
- didChangeObject
..with message: NSFetchedResultsChangeUpdate
..which ran:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
- controllerDidChangeContent:
[self.tableView endUpdates];
那偉大工程的其他視圖 - 控制,經過記錄被添加到數據庫後立即這個序列。
ListCourses VC:
- didChangeSection
...with message: NSFetchedResultsChangeInsert
...which ran:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
- didChangeObject
..with message: NSFetchedResultsChangeInsert
..which ran:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
爲什麼在一個視圖 - 控制得到NSFetchedResultsChangeInsert消息,但另一個不?
以下是錯誤視圖控制器的委託方法。
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
//[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
//[tableView reloadData];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
// [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
NSLog(@"vc>>> about to reload data");
// [self.tableView reloadData];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
// [self.tableView reloadData];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);
[self.tableView endUpdates];
}
謝謝, 菲爾
謝謝羅賓!我會繼續,並在早上讓你知道......凌晨1點! – phil
全部請參考原始問題結尾的更新信息。我已經提供了tableview委託方法和我通過斷點和調試器看到的調用流程。任何想法爲什麼我的vcSimulator tableview委託方法永遠不會得到NSFetchedResultsChangeInsert消息? – phil
上面編輯的答案。 –