我遇到的 FRC 和 TableView 委托行为似乎不一致:
- 对performFetch的初始调用会导致按预期调用委托方法。但是,如果我更新 FRC 的baseFetch上的谓词,然后再次调用performFetch,则永远不会调用 FRC 委托方法,并且不会使用新数据更新 TableView。为了强制表更新和显示新数据,我显式调用reloadData。
- 对performFetch的初始调用正确地构建了 TableView 的部分。如果我更改 FRC(新的 FCR 具有不同的fetchRequest和sectionNameKeyPath)并再次调用performFetch,则表和部分会按预期更新以匹配新结果。但是,如果我随后返回原始 FRC 并调用performFetch,则这些部分具有正确的名称,但具有先前 FRC 的部分/行结构。为了强制更新这些部分,我明确调用reloadSectionIndexTitles。
我的感觉是,如果有什么变化,代表应该在我们调用performFetch的任何时候被解雇。显式调用reloadData和reloadSectionIndexTitles似乎是一个昂贵且不必要的步骤,因为这就是代表存在的目的。我错过了什么吗?
以下是相关代码:
NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.myTable beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[self.myTable cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[self.myTable reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.myTable insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.myTable deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.myTable endUpdates];
}
UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [[currentFCR sections] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[currentFCR sections] objectAtIndex:section];
return section.name;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[currentFCR sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
抓取
- (NSFetchedResultsController *)FRC1 {
if (FRC1 != nil) {
return FRC1;
}
NSFetchRequest *request = [[DataProvider instance] getFetch];
[[DataProvider instance] sortListByFirstSort:request];
[request setFetchBatchSize:20];
FRC1 = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:[[DataProvider instance] managedObjectContext]
sectionNameKeyPath:@"groupName"
cacheName:nil];
return FRC1;
}
- (NSFetchedResultsController *)FRC2 {
if (FRC2 != nil) {
return FRC2;
}
NSFetchRequest *request = [[DataProvider instance] getFetch];
[[DataProvider instance] sortListBySecondSort:request];
[request setFetchBatchSize:20];
FRC2 = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:[[DataProvider instance] managedObjectContext]
sectionNameKeyPath:@"name"
cacheName:nil];
return FRC2;
}
-(void)doFetch{
NSError *error;
if (![currentFCR performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self.myTable reloadData];
[self.myTable reloadSectionIndexTitles];
}