NSFetchedResultsController: Multiple FRCs, Delegate Error when Updating

前端 未结 2 1829
孤城傲影
孤城傲影 2020-12-12 06:21

Objective: Using FRC, sort Section\'s by startDate, an NSDate attribute, but want Today\'s date Se

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 06:47

    In your ThreeFRC project there are some issues:

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView beginUpdates];
        self.numberOfSectionsInTV = 0;
        [self fetchData];
    }
    
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView reloadData];
        [self.tableView endUpdates];
    }
    

    You shouldn't use fetchData inside FRC delegate. Methods are called in proper order (before, during and after update) so inside callbacks you have consistent state of context. Also it's not the best idea to use reloadData before endUpdates(it's applying all changes you provided earlier) and reloadData is erasing everything and building it from scratch. This is most likely causing the crash.

    Other thing I've spotted that may be buggy is handling of updates. If you have 3 separate FRC without sections you won't get section update callback in FRC delegate. But if some objects appear in one of the FRC's then you should detect that and manually insert them.

    Using just reloadData in controllerDidChangeContent would be enough, but this isn't the best solution, as you won't get any animations. The proper way would be to handle all the cases: deleting all objects from one of FRCs (and then deleting section manually from TableView), inserting first object into FRC (then you should create new section at proper indexPath).

提交回复
热议问题