无理由从已提取的结果中删除了托管对象

时间:2012-10-22 00:33:34

标签: ios core-data nsmanagedobject nsfetchrequest nsfetchedresultscontroller

我有一个奇怪的问题,我无法弄清楚。我有一个相当典型的UIViewController来自SQLite商店的UITableViewNSFetchedResultsController个抓取对象。提取工作正常,表格工作正常,具有典型的NSFetchedResultsController样板:

-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller{

    [self.tableView beginUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{

    if(type==NSFetchedResultsChangeInsert){
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
    } else if(type==NSFetchedResultsChangeDelete){
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
    }

}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{

    UITableView *tV = self.tableView;
    if(type==NSFetchedResultsChangeInsert){
        [tV insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if(type==NSFetchedResultsChangeDelete){
        [tV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if(type==NSFetchedResultsChangeUpdate){
        [self configureCell:[tV cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
    } else if(type==NSFetchedResultsChangeMove){
        [tV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tV insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
    [self.tableView endUpdates];
}

我遇到的问题是任何关系为nil的对象(即使关系属性未包含在谓词或排序描述符中的提取请求中)也会从获取的结果中消失(controller:didChangeObject:每次以任何方式更新时都会调用NSFetchedResultsChangeDelete}(例如更改不相关的属性)。如果我重新执行提取,对象将会返回。

即使我在提取请求中没有使用谓词,也会发生这种情况。

有人知道这里会发生什么吗?

1 个答案:

答案 0 :(得分:0)

我能够通过更改NSFetchedResultsController上的sectionNameKeyPath来解决此问题。如果sectionNameKeyPath属性是可选关系属性,请尝试删除它或更改它。

如果您实际上想要使用该属性来分隔各个部分,则可能需要使用“虚拟”对象来表示nil。 (这实际上意味着如果您将属性用于您的部分,则该属性不能是可选的。)

相关问题