简体   繁体   中英

Managed Object Removed from Fetched Results For No Reason

I've got an odd problem that I can't figure out. I have a fairly typical UIViewController with a UITableView and an NSFetchedResultsController fetching objects from an SQLite store. The fetching works fine and the table works fine, with typical NSFetchedResultsController boilerplate:

-(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];
}

The problem I'm having is that objects with any relationships that are nil (even if the relationship property is not involved in the fetch request in either the predicate or sort descriptors) disappear from the fetched results ( controller:didChangeObject: gets called with NSFetchedResultsChangeDelete ) any time they are updated in any way (changing an unrelated property for instance). If I re-perform the fetch, the object will be back.

This happens even if I don't use a predicate in the fetch request.

Does anybody have any idea what might be going on here?

I was able to fix this issue by changing my sectionNameKeyPath on my NSFetchedResultsController. If your sectionNameKeyPath property is an optional relationship property, try removing it or changing it.

You may need to use a "dummy" object to represent nil if you in fact want to use that property to separate the sections. (Which in effect means the property can't be optional if you will be using it for your sections.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM