2010-10-03 146 views
0

的更改我对核心数据的常规使用模式非常熟悉,但最近偶然发现了一个问题:想象一下,我有一个人的实体,有2个字符串属性名称和公司的简单案例。我想让UITableView按名称排序并按公司名称划分。听起来很简单:核心数据:对

... 
personFetchController_ = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                  managedObjectContext:mainDataCenter.managedObjectContext 
                   sectionNameKeyPath:@"company" 
                     cacheName:@"PersonListCache"];  
NSError *error = nil; 
if (![personFetchController_ performFetch:&error]) 
{ 
    LogError(@"Error fetching persons: %@", [error localizedDescription]); 
} 
personFetchController_.delegate = self; 

我注册不如授人以听变化,特别是更改为部分:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
      atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 
    NSIndexSet *sections = [NSIndexSet indexSetWithIndex:sectionIndex]; 
    switch (type) 
    { 
     case NSFetchedResultsChangeInsert: 
      [personTableView_ insertSections:sections withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [personTableView_ deleteSections:sections withRowAnimation:UITableViewRowAnimationFade]; 
     default: 
      break; 
    } 
} 

它非常好,当我添加/删除/改变一个人的名字,但如果我更改了一个人的公司名称(意思是从一个部分移动到另一个部分),应用程序崩溃,在插入后说,部分中的行数需要是旧值加1。

任何人都有这个工作权利?

回答

0

导致此崩溃的代码最有可能不是此方法。

请张贴此方法的代码:

-(void)controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: 

尤其是你如何用一种NSFetchedResultsChangeMove的变化做出反应?

在苹果的文档NSFetchedResultsControllerDelegate他们用这个didChangeObject

:...

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

之前,我发现了苹果的文档包含了几乎所有必要的代码NSFetchedResultsController我使用了一些代码,从网络,我想从一些教程,这在罕见的事件中导致崩溃。过了一段时间后,我发现在将第一部分中的唯一对象(将被删除)移动到第二部分(这将是新的第一部分)时,我可以触发此操作。

从此我在阅读教程之前先阅读并搜索Apple-Documentation ^^