如何刷新NSFetchedResultsController? [英] How can I refresh a NSFetchedResultsController?

查看:225
本文介绍了如何刷新NSFetchedResultsController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSFetchedResultsController,它在UITableView中显示数据。选择创建Core Data项目时,我使用的是Xcode提供的样板代码。我将以下谓词添加到NSFetchedResultsController使用的NSFetchRequest中(在初始化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 predicateWithFormat:@"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

当我返回TableViewController时,它仍然显示此已删除行。

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

当我尝试重新加载表格视图,什么也没有发生。

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

当我尝试使用 performFetch 重新加载fetchedResultsController时,它说:

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


'致命错误:部分信息的永久高速缓存与当前配置不匹配。您已非法更改了NSFetchedResultsController的获取请求,其谓词或其排序描述符,而没有禁用缓存或使用+ deleteCacheWithName:'

'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:'

如果我删除了缓存,在init方法中,调用 performFetch ,然后调用 [myTable reloadData] 起作用。

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

有没有更简单的方法来刷新数据?最好允许您使用NSFetchedResultsController的缓存功能?

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.

更新:现在,这与分配和初始化NSFetchedResultsController的方法相同,因此它显示的错误消息似乎不正确。我更了解 NSFetchedResultsController ,我知道它不会自动为您删除行,它是 controller:didChangeObject:atIndexPath:forChangeType :nowIndexPath:方法,主要用于删除行。我实施了此方法,因为我在项目中使用了Apple的模板。

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.

但是,对于我来说,我实际上并没有删除项目,而是在更新属性(已删除)以指定该项目不再存在于列表中。这意味着 controller:didChangeObject:atIndexPath:forChangeType:nowIndexPath:方法的更改类型为 NSFetchedResultsChangeUpdate 而不是 NSFetchedResultsChangeDelete 。我将代码更新为以下内容:

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:


无效的更新:第0节中的行数无效。更新(3)之后必须包含在现有节中的行数等于更新(3)之前该节中包含的行数,加上或减去从该节中插入或删除的行数(已插入1个,删除了2个)。与userInfo(空)

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)

基本上,它抱怨的是NSFetchedResultsController中的对象数量和表中的单元格数量不一样

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

希望这更加清楚:

// 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]

即使NSFetchedResultsController的NSFetchRequest的NSPredicate设置为 deleted == NO , NSFetchedResultsController仍然看到3个项目,而不是2个。

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

如何解决此问题?我是否需要以某种方式刷新NSFetchedReultsController?

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

推荐答案

问题是我没有意识到已删除<在这种情况下,/ code>是保留字。我不得不重命名该字段,并且效果很好。

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.

这篇关于如何刷新NSFetchedResultsController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆