Je connais très bien les habitudes d'utilisation des données de base, mais je suis récemment tombé sur un problème: Imaginez un cas simple où j'ai une entité de personne, avec deux attributs de chaîne. Je veux faire un UITableView trié par noms et divisé en sections par nom de l'entreprise. Un son assez simple:Données de base: changements à la section
...
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;
je me inscrire en tant que délégué à l'écoute des changements, notamment les changements aux sections:
- (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;
}
}
Il fonctionne très bien quand je ajouter/supprimer/modifier le nom d'une personne , mais si je change le nom de la société (ce qui signifie de passer d'une section à l'autre), l'application se bloque, en disant après une insertion, le nombre de lignes dans la section doit être l'ancienne valeur plus un.
Tout le monde a bien compris?