I am currently having an issue where my UITableViewController/UITableView which uses an NSFetchedResultsController displays about 86 items when the fetchLimit on the fetchRequest for the FRC is 4. I know that 86 items satisfy the fetch itself, and I know that the reason that they appear is because the didChangeObject: atIndexPath... gets called for each of the 86, and I insert as is kind of the default implementation.
My question is why doesn't the fetchLimit limit the number of objects that the NSFetchedResultsController tries to "change" (insert in this case)?
My application use case is that the first tab displays typical feed items which I get (on a side thread) at app launch. I save them to CoreData on a separate context, which eventually merges to the main thread's context and initiates the FRC callbacks for what changed. My question pertains specifically to the initial case where no items exist.
Here is my fetchRequest:
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:ENTITY_CONTENT_ITEM inManagedObjectContext:managedObjectContext];
// Set a limit on the number of items returned
[fetchRequest setFetchLimit:4];
// Set a Predicate to limit the fetch to featured items only
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"featured == YES AND contentType == %d", contentType]];
// Set the sort descriptors
NSSortDescriptor *sortDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sortDate" ascending:NO] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDateDescriptor]];
The contentType above is simply a way to break out what is supposed to display in this tab versus other tabs. Featured is a boolean property on the item, which is more like an on off switch for display purposes.
Here is my didChangeObject:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[tableView cellForRowAtIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
I know it's going to be hard to answer this question, but even an explanation of how the FRC decides how many times to call didChangeObject would be really helpful.