NSFetchedResultsController to only get first 10 items

I'm using an NSFetchedResultsController (and the delegate methods) to display items from CoreData into a UITableView.

However, I only want the table to show the first few items (say 10).

To do this I have done this...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return MIN([[self.fetchedResultsController fetchedObjects] count], 10);
}

This will return 10 if there are more than 10 items. Or it will return the number of items if it is less than 10.

It works for the first 10 items downloaded. However, I then triggered a refresh of the downloaded data.

This added another 10 items to CoreData and then triggered the FRC delegate methods.

This caused an error saying...

There were 10 items before the update. You added 10 items. Now there are still 10 items but there should be 20.

This came from the UITableView beginUpdates and endUpdates method.

I know that it is right as I am adding stuff to CoreData but I only still want it to show the first 10.

Is there an entirely different way of doing this? Or can I fix the delegate methods somehow?

Answers


Rather than using the begin and end updates methods, just reload the table view. This means you aren't telling the table about new rows (and how many there are).

Also, on the FRC, be sure to set the fetch limit on the fetch request so it doesn't fetch more than required.


Need Your Help

Rendering the elements of a GXT RadioGroup or CheckBoxGroup in multiple columns?

javascript extjs gwt-ext gxt

I am looking for a way to render the elements of a GXT (GWT-Ext) RadioGroup or CheckBoxGroup in a layout other than a single column (Orientation.VERTICAL) or a single row (Orientation.HORIZONTAL). ...

Allow a prerequisite to be installed only on Windows 8 using Advanced Installer

sqlite prerequisites advanced-installer

I have developed an installer using Advanced Installer.I want that the SQLite package that I have added as a prerequisite should only be shown on Windows 8 and not on any other Operating Systems.Ca...