NSFetchedResultsController ignores fetchLimit after executing Fetch

I have a tabbed application that has 2 tabs with 2 UITableView.
I also have 2 objects of type NSFetchedResultsController, but both of them are on the same object with a different order and a different selection limit.
When I download more objects from the Internet and paste them into the database, objects like NSFetchedResultsController will ignore fetchLimit. For the first, I installed fetchLimit out of 10, and for the second I installed fetchLimit out of 50. First I have 10 objects in the database. Everything is fine. After I load more than 40 objects, the first also loads more than 40 objects, but it has a fetchLimit value of 10.

What happened to this?

+4
source share
2 answers

NSFetchedResultsController ignores fetchLimit when observer context changes.

I think this is not an easy operation to update the table correctly through momc observation when you are limited to fetchlimit.

SOLUTION # 1

So, in case a big update has happened, you have to retrieve the data again.

So you should do something like this in the 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]; } } 
+2
source

The NSFetchRequest fetch limit is the limit used when fetching from the data warehouse. It is not intended to limit the size of the resulting array of results. If you want to limit the total number of cells displayed in your table view, you should probably use UITableView tableView:numberRowsForSection: to limit it.

If you really want to set a limit of only 10 elements (for example, showing the Top 10 list), there is probably no real reason to use more than one section. If you limit the number of sections in the View table to 1 using numberOfSectionsInTableView: and the number of rows in this AT MOST 10 section with tableView:numberRowsForSection: your table will always display up to 10 records, regardless of how many records have been added to the data store.

You can use sort descriptors to order objects controlled by an instance of NSFetchedResultsController, which I think is useful for displaying a Top 10 list.

-2
source

Source: https://habr.com/ru/post/1393222/


All Articles