开发者

When reordering a UITableView, NSFetchedResultsController doesn't have latest state

开发者 https://www.devze.com 2023-02-10 04:31 出处:网络
I have a UITableView powered by an NSFetchedResultsController and am trying to properly support reordering.

I have a UITableView powered by an NSFetchedResultsController and am trying to properly support reordering.

Inside moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath I respond to the reordering by updating an order value on the NSManagedObjects and save the context like this:

[self.fetchedResultsController.managedObjectContext save:nil];

So now all objects have the correct order value and the results controller's NSFetchRequest sorts by that value correctly.

This does appear to work, however when I scroll up and down in the table view, the old values are being displayed. So it looks like when I use objectAtIndexPath on the NSFetchedResultsController, it doesn't return the latest object.

- (UITableViewCell *)tableView:(开发者_如何学编程UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  // ... cell generation code ...

  NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
  [cell.textLabel setText:sideEffect.name];

}

If I navigate away and then return, the cells are in the correct order.

After my call to save, do I need to:

  • Refresh the NSFetchedResultsController
  • Invalidate a cache
  • Refresh/reset something on the UITableView?


The solution was to reinitialise the fetch controller after the move modifications had taken place. Since I had a special getter method generating the controller (and performing the first fetch), I could just set the instance variable to nil to cause it to be regenerated next time it was needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号