2010-08-24 32 views
0

我正在使用NSFetchedResultsController填充我的UITableViewController的内容。刷新和删除后台线程错误(NSFetchedResultsController)内容

我使用的NSOperation从web服务(我使用分离ManagedObjectContext,因为它是另一个线程)
当数据被保存我的视图控制器(至极是NSFetchedResultsControllerDelegate)收集的数据被调用,我合并我的使用mergeChangesFromContextDidSaveNotification

#pragma mark - 
#pragma mark Parsers delegate 

- (void)parserWillSave:(id)parser{ 
    TopNewsParser *emp = (TopNewsParser *)parser; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(parserContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:emp.managedObjectContext]; 
    [NSFetchedResultsController deleteCacheWithName:@"aLaUne"]; 
} 

- (void)parserDidSave:(id)parser{ 
    TopNewsParser *emp = (TopNewsParser *)parser; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:emp.managedObjectContext]; 
} 

/** 
Notification from the add controller's context's save operation. This is used to update the fetched results controller's managed object context with the new book instead of performing a fetch (which would be a much more computationally expensive operation). 
*/ 
- (void)parserContextDidSave:(NSNotification*)saveNotification { 
    DLog(@""); 
    NSManagedObjectContext *fetchContext = [_fetchedResultsController managedObjectContext]; 
    // Merging changes causes the fetched results controller to update its results 
    [fetchContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) 
            withObject:saveNotification 
           waitUntilDone:YES]; 


} 

MOCS对于NSFetchedResultsControllerDelegate我正在使用的代码从CoreData书籍样本

#pragma mark - 
#pragma mark NSFetchedResultsControllerDelegate 

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 

    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    //ALog(@"indexPath: %@ newIndexPath : %@ | type : %d # %@",indexPath,newIndexPath,type,anObject); 
    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]; 
      // Reloading the section inserts a new row and ensures that titles are updated appropriately. 
      [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 

} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

    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 { 
    UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
                        target:self        
                        action:@selector(refreshTableViewContent)]; 
    reloadButton.accessibilityLabel = @"Reload"; 
    self.navigationItem.leftBarButtonItem = reloadButton; 

    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    [self.tableView endUpdates]; 

} 

我的问题是当加载新的内容,其中一些对象删除,我的tableView搞砸了!

然后当我向下滚动,我的tableview是空的(只有4个可见行)仍然在tableview中,否则这是一个空白的滚动视图

在:即使我的countOfRow减少1行仍然可见控制台我可以看到以下消息。

严重的应用程序错误。在调用-controllerDidChangeContent:期间,NSFetchedResultsController的委托捕获到异常。 *** - [NSMutableArray的removeObjectAtIndex:]:索引0超越界限的空数组与用户信息(空)

在我因子评分,这是由于我的NSFetchedResultsController的缓存,但即使我禁用它,我有beggining一样的问题。

有人对如何解决这个问题有想法吗?

回答