NSFetchedResultsController tries to limit the number of records displayed

When creating an NSFetchRequest to go to my NSFetchedResultsController I set the fetchLimit property to 3.

Now initially it looks fine. I can change the first three returned objects in any way that changes their order, and they are all shuffled correctly. The problem arises when I either modify the object that originally fell in the first three, in a way that now transfers it to the first three, or simply adding a new object so that it appears during the first three.

What I expected to see: a nested object pushes the others down and one drops down.

What actually happens: The nested object pushes the rest, and the number of entries increases to 4 ?!

Can someone explain this or how should I handle this?

0
ios core-data nsfetchedresultscontroller nsfetchrequest
source share
1 answer

I made some progress by numberOfObjects around this, basically ignoring numberOfObjects and returning the actual length, I want the table to be committed. This requires a bit of cheating in controller:didChangeObject:... , but it seems to work so far.

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return kTableSize; //return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.myTableView; switch(type) { case NSFetchedResultsChangeInsert: // Only modify table if insert will effect visible rows if (newIndexPath.row < kTableSize) { // Delete last row to maintain fixed length [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } break; case NSFetchedResultsChangeDelete: // Only modify table if delete will effect visible rows if (indexPath.row < kTableSize) { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // Insert extra row to maintain fixed length [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; } break; case NSFetchedResultsChangeUpdate: // Only modify table if update will effect visible rows if (indexPath.row < kTableSize) { [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } break; case NSFetchedResultsChangeMove: // Only modify table if move will effect visible rows if ((indexPath.row < kTableSize) || (newIndexPath.row < kTableSize)) { // Delete old row or last row of table if (indexPath.row < kTableSize) { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } else { [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; } // Insert new row or a row at bottom of table if (newIndexPath.row < kTableSize) { [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } else { [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; } } break; } } 

You also need to take care of tableView:cellForRowAtIndexPath: to make sure that we are not trying to access an object that does not exist if there are fewer objects than the length of the table.

+7
source share

All Articles