Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:00:52+00:00 2026-05-16T08:00:52+00:00

I’m using an NSFetchedResultsController in a standard way to update a UITableView whenever something

  • 0

I’m using an NSFetchedResultsController in a standard way to update a UITableView whenever something changes in the related core-data entities. I’m doing the same as described in the Apple documentation.

The problem I have is when I make a mass insert of new core-data entities. This causes the NSFetchedResultsController delegate to allocate (and insert) a new cell for each entity, but it does this without recycling the UITableViewCells (i.e., dequeueReusableCellWithIdentifier: always returns null). This means the allocation of potentially 100s of UITableViewCells, which can lead to memory problems. Does anyone know of a fix or workaround? Thanks.

Edit 1:

Within my UITableViewController subclass I have the standard NSFetchedResultsControllerDelegate methods. I believe this is identical to the example from Apple.

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationTop];
            break;
    }
}

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    switch(type) {          
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationTop];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationBottom];
            break;
    }
}

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}

Also within my UITableViewController subclass I have the following for fetching a cell for a given indexPath:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Waypoint *waypoint = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [waypoint comment];
}

-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *CellIdentifier = @"WaypointCell"; // matches identifier in XIB

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSLog(@"new cell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];      
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        NSLog(@"recycled cell");    
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

In the tableView:cellForRowAtIndexPath: function I added NSLog statements to display what’s going on.

Here is what’s happening. The above mentioned UITableViewController is pushed onto the navigation stack. An asynchronous request goes out and fetches a bunch of data, and creates or modifies the data related to this fetchController. Once [self.tableView endUpdates] gets called, the system begins creating and inserting the UITableViewCells into the UITableView. In the debugger console the output “new cell” is printed multiple times (can number in the 100s), which I believe is one for each new entity created. Only after the tableview is loaded (if it didn’t crash due to memory problems) and I begin scrolling do I see the “recycled cell” output in the console.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:00:53+00:00Added an answer on May 16, 2026 at 8:00 am

    I found a solution I’m happy with. The problem isn’t NSFetchedResultsController per se, rather that it calls [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop] potentially hundreds of times from the NSFetchedResultsController delegate.

    My solution is to introduce a massUpdate boolean that controls whether the NSFetchedResultsController should insert the new table rows as above, or whether it should do a simple [tableView reloadData] instead. I set this depending on the number of rows I plan to insert. Details of the implementation can be found at the following forum post:

    https://devforums.apple.com/message/181219#181219

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.