开发者

How can I refresh a NSFetchedResultsController?

I have a NSFetchedResultsController which displays data in a UITableView. I'm using the boiler plate code provided by Xcode when choosing to create a Core Data project. I added the following predicate to the NSFetchRequest the NSFetchedResultsController uses (before NSFetchedResultsController is initialized):

NSPredicate *predicate = [NSPredicate pre开发者_高级运维dicateWithFormat:@"deleted == NO"];
[fetchRequest setPredicate:predicate];

Now in another location of my app, I set the deleted property like so (pseudo-code):

myManagedObject.deleted = YES
saveDataContext

When I return to the TableViewController, it still shows this "deleted" row.

When I try to reload the table view, nothing happens.

When I try to reload the fetchedResultsController using performFetch, it says:

'FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'

If I remove the caching, in the init method, call performFetch, then call [myTable reloadData] it works.

Isn't there a simpler way to refresh the data? Preferably one that allows you to use the caching feature of NSFetchedResultsController?

As far as I know, the only place I am modifying the fetch request, predicate, or sort descriptor is in the same method that allocs and inits the NSFetchedResultsController, so it seems that the error message that it displays is incorrect.

Update: Now that I understand the NSFetchedResultsController a bit better, I understand that it won't remove rows for you automatically, and it's the controller:didChangeObject:atIndexPath:forChangeType:nowIndexPath: method that is primarily responsible for deleting rows. I had this method implemented, since I used Apple's template for my project.

However, in my case I'm not actually deleting an item, I'm just updating a property (deleted) to specify that the item should no longer exist in the list. This means that the controller:didChangeObject:atIndexPath:forChangeType:nowIndexPath: method's change type is NSFetchedResultsChangeUpdate and not NSFetchedResultsChangeDelete. I updated the code to something such as:

  case NSFetchedResultsChangeUpdate: {
    MyObj *obj = (MyObj *)anObject;
    if (obj.deletedValue) { // NOTE: deletedValue returns the deleted property as BOOL
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else {
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
      break;
    }
  }

The problem with this is that I get the error message:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 2 deleted). with userInfo (null)

Basically it's complaining that the number of objects in the NSFetchedResultsController and the number of cells in the table aren't synced.

Hopefully this makes it clearer:

// DB Items (Name, Deleted):
[Foo, NO]
[Bar, NO]
[Baz, NO]

// mark one as deleted
Objects[1].deletedValue = YES;

// DB items now look like so:
[Foo, NO]
[Bar, YES]
[Baz, NO]

Even though NSFetchedResultsController's NSFetchRequest's NSPredicate is set to deleted == NO, the NSFetchedResultsController still sees 3 items instead of 2.

How can I solve this issue? Would I need to refresh the NSFetchedReultsController somehow? What else could be the problem?


The problem was that I didn't realize that deleted is a reserved word in this case. I had to rename the field and it worked fine.


You need to setup a delegate to the NSFetchedRequestController in order to keep track of changes. To handle batch updates you need to override three methods:

  • controllerWillChangeContent: - (Setup lists of index paths here)
  • controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: - (Store for each call here)
  • controllerDidChangeContent: (Excute updates to UITableView here.)

For the much simpler case where you know that only rows will always be updated one at a time, then go for just something like this:

-(void)controller:(NSFetchedResultsController *)controller 
  didChangeObject:(id)anObject 
      atIndexPath:(NSIndexPath *)indexPath 
    forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath;
{
    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                                  withRowAnimation:UITableViewRowAnimationTop];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationTop];
            break;
        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


It seems pretty clear to me, from the error message, that an NSFetchedResultsController does not expect you to change its fetch request once it is created.

What you need to do is add the predicate to the fetch request before the NSFetchedRequestController is initialised. If you want to change the predicate of the fetch request while the app is running, you probably need to create a new NSFetchedRequestController and replace the old one completely.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜