NSFetchedResultsController:更改谓词不起作用?

发布于 2024-08-25 16:23:38 字数 780 浏览 4 评论 0原文

我正在编写一个应用程序,一个屏幕上有两个表格。左表是文件夹列表,右表显示文件列表。当点击左侧的一行时,右侧的表格将显示属于该文件夹的文件。

我正在使用核心数据进行存储。当文件夹的选择发生变化时,右表的 NSFetchedResultsController 的 fetch 谓词将发生变化并执行新的 fetch,然后重新加载表数据。我使用了以下代码片段:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list = %@",self.list];
[fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {   
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
[table reloadData];

但是获取结果仍然相同。我在获取之前和之后都对“谓词”进行了 NSLog 编辑,并且更新的信息是正确的。获取结果与初始获取相同(加载视图时)。

我不太熟悉 Core Data 获取对象的方式(是否有缓存系统?),但我之前用单个表视图做过类似的事情(更改谓词、重新获取数据和刷新表),一切都顺利出色地。

如果有人能给我一个提示,我将不胜感激。

提前致谢。

I'm writing an app with two tables on one screen. The left table is a list of folders and the right table shows a list of files. When tapped on a row on the left, the right table will display the files belonging to that folder.

I'm using Core Data for storage. When the selection of folder changes, the fetch predicate of the right table's NSFetchedResultsController will change and perform a new fetch, then reload the table data. I used the following code snippet:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list = %@",self.list];
[fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {   
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
[table reloadData];

However the fetch results are still the same. I've NSLog'ed "predicate" before and after the fetch, and they were correct with updated information. The fetch results stay the same as initial fetch (when view is loaded).

I'm not very familiar with the way Core Data fetches objects (is there a caching system?), but I've done similar things before(changing predicates, re-fetching data, and refreshing table) with single table views and everything went well.

If someone could gave me a hint I would be very appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看春风乍起 2024-09-01 16:23:38

我几乎完全遇到了这个问题,直到我在最近的一篇博客文章中找到了提示 iPhone 孵化器

NSFetchedResultsController 正在缓存第一个结果。 (您可能在 initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName 处设置了一些内容)

我猜测您的代码(像我的代码一样)是 CoreData 示例的派生,因此假设它是 @"Root",在更改谓词之前,执行

[NSFetchedResultsController deleteCacheWithName:@"Root"];  

I had almost exactly this problem, until I found the hint in a very recent blog post at iphone incubator

NSFetchedResultsController is caching the first results. (You probably have something set at initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName)

I'm guessing your code (like mine) is a derivation of the CoreData sample, so assuming it's @"Root", before you change the predicate, do a

[NSFetchedResultsController deleteCacheWithName:@"Root"];  
梦巷 2024-09-01 16:23:38

这对我有用:

[NSFetchedResultsController deleteCacheWithName:nil];  
[self.fetchedResultsController.fetchRequest setPredicate:predicate]; 

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"%@, %@", error, [error userInfo]);
    abort();
} 

This worked for me:

[NSFetchedResultsController deleteCacheWithName:nil];  
[self.fetchedResultsController.fetchRequest setPredicate:predicate]; 

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
    NSLog(@"%@, %@", error, [error userInfo]);
    abort();
} 
指尖上的星空 2024-09-01 16:23:38

就我而言,deafgreatdane 的答案不起作用;我所做的可能很天真,但有效:

  • configureCell:atIndexPath: 方法中创建相应的谓词(在 tableView:cellForRowAtIndexPath: 中调用),
  • 然后致电:
    [_fetchedResultsController.fetchRequest setPredicate:predicate];
    [_fetchedResultsController PerformFetch:nil];
  • 通过调用[self.fetchedResultsController objectAtIndexPath:indexPath]引用返回的对象

In my case deafgreatdane's answer isn't working; what I did instead is probably naive, but works:

  • create corresponding predicate(s) in the configureCell:atIndexPath: method (called in tableView:cellForRowAtIndexPath:)
  • then call:
    [_fetchedResultsController.fetchRequest setPredicate:predicate];
    [_fetchedResultsController performFetch:nil];
  • refer to the returned object by calling [self.fetchedResultsController objectAtIndexPath:indexPath]
话少心凉 2024-09-01 16:23:38

我的 swift 项目中也有类似的问题。这对我有用

NSFetchedResultsController.deleteCache(withName: fetchedResultsController.cacheName)

Had a similar issue in my swift project. This worked for me

NSFetchedResultsController.deleteCache(withName: fetchedResultsController.cacheName)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文