Search code examples
objective-ccore-dataios5nsfetchedresultscontroller

NSFetchedResultsController ignores fetchLimit after performFetch


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?


Solution

  • 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];
    
       }
    }