4

I have a tabbed application, that has 2 tabs with 2 UITableView.
I also have 2 NSFetchedResultsController type objects, but both of them are on the same entity with different ordering and different fetch limit.
When I download more objects from the internet and insert them to the database, my NSFetchedResultsController type objects will ignore the fetchLimit. For the first one I set a fetchLimit of 10 and for the second I set a fetchLimit of 50. Initially I have 10 objects in the database. Everything is fine. After I download more 40 objects the first one also loads the more 40 objects, but it has a fetchLimit of 10.

What's wrong with this?

Infinite Possibilities
  • 7,415
  • 13
  • 55
  • 118

2 Answers2

2

NSFetchedResultsController ignoring fetchLimit in case if it observers context changes.

I think that it's not so simple operation to correctly update table via momc observation, when you're restricted to fetchlimit.

SOLUTION #1

So, in case if big update has been occured, you should re-fetch data.

So you should do something like this in FRC delegate:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
   [self.tableView endUpdates];
   if (bigChangesPeformed) {
     NSError * error;

     // Re-fetching to get correct fetch limit
     [self.fetchedResultsController performFetch:&error];
     if (error) {
        // bla-bla-bla
     }
     [self.tableView reloadData];

   }
}
tt.Kilew
  • 5,954
  • 2
  • 33
  • 51
  • It seems to be ok, but not very efficient, because I have to refetch every time this happens. I see you wrote there Solution #1, do you know any other solution? – Infinite Possibilities Jan 26 '12 at 23:23
  • I wrote Solution #2, rechecked it and then deleted it :) I was trying to override -tableView:numberRowsForSection: method to MIN(fetchSize, realItemsCount), But that has no success. – tt.Kilew Jan 27 '12 at 10:50
-2

The fetch limit of NSFetchRequest is the limit used when performing fetches from the data store. It's not intended to limit the size of the fetched results array. If you want to limit the total number of cells displayed in your tableview, you should probably use UITableView's tableView:numberRowsForSection: to limit it.

If you really want to set a limit of only 10 items displayed (like showing a Top 10 list), there's probably no real reason to use more than 1 section either. If you restrict the number of sections in your tableView to 1 using numberOfSectionsInTableView: and the number of rows in that section AT MOST 10 with tableView:numberRowsForSection:, your table will always show up to 10 records, regardless of how many records are added into the data store.

You can use sort descriptors to order the objects managed by your NSFetchedResultsController instance, which I imagine is useful for displaying a Top 10 list.

Thomas Verbeek
  • 2,361
  • 28
  • 30