Hé les gars, donc j'ai mon NSFetchedResultsController fonctionnant correctement sous le 3.1 SDK, mais je commence à obtenir des erreurs étranges, en particulier dans les méthodes de délégué quand j'essaye sous 3.0. J'ai déterminé que cela est lié aux méthodes NSFetchedResultsControllerDelegate. C'est ce que j'ai mis en place. Le truc inEditingMode est lié à la façon dont j'ai implémenté l'ajout d'une autre section statique à la table.NSFetchedResultsController - Déléguer des méthodes se brisant sous iPhone OS 3.0, mais pas sous 3.1
- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex];
if(self.inEditingMode){
sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex + 1];
}
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
break;
default:
[self.tableView reloadData];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
NSIndexPath *relativeIndexPath = indexPath;
NSIndexPath *relativeNewIndexPath = newIndexPath;
if(self.inEditingMode){
relativeIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1];
relativeNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section + 1];
}
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:relativeNewIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:relativeIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
[self.tableView reloadData];
break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
[self.tableView endUpdates];
}
Lorsque j'ajoute une entité dans le contexte d'objet géré, je reçois l'erreur suivante:
Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
Je mis un point d'arrêt sur objc_exception_throw, et l'accident semble être survenant à l'intérieur de controllerDidChangeContent.
Si je commente toutes les méthodes self.tableView et que je place un seul [self.tableView reloadData] dans controllerDidChangeContent, tout fonctionne comme prévu.
Quelqu'un at-il une idée de la raison pour laquelle cela se produit?
J'ai oublié d'ajouter dans mon post initial, que j'avais déjà implémenté cela, mais le problème persistait toujours. –
En fait, après un examen plus approfondi, et la mise en œuvre de cette correction à nouveau, le problème a disparu. Je dois avoir fait quelque chose de bizarre la dernière fois. Merci beaucoup! –
Heureux d'entendre que ça a été réparé. Les erreurs Core Data et NSFetchedResultsController peuvent être assez difficiles à diagnostiquer et à corriger. – glorifiedHacker