NSFetchedResultsController:更改谓词不起作用?
我正在编写一个应用程序,一个屏幕上有两个表格。左表是文件夹列表,右表显示文件列表。当点击左侧的一行时,右侧的表格将显示属于该文件夹的文件。
我正在使用核心数据进行存储。当文件夹的选择发生变化时,右表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我几乎完全遇到了这个问题,直到我在最近的一篇博客文章中找到了提示 iPhone 孵化器
NSFetchedResultsController 正在缓存第一个结果。 (您可能在 initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName 处设置了一些内容)
我猜测您的代码(像我的代码一样)是 CoreData 示例的派生,因此假设它是 @"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
这对我有用:
This worked for me:
就我而言,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:
configureCell:atIndexPath:
method (called intableView:cellForRowAtIndexPath:
)[_fetchedResultsController.fetchRequest setPredicate:predicate];
[_fetchedResultsController performFetch:nil];
[self.fetchedResultsController objectAtIndexPath:indexPath]
我的 swift 项目中也有类似的问题。这对我有用
NSFetchedResultsController.deleteCache(withName: fetchedResultsController.cacheName)
Had a similar issue in my swift project. This worked for me
NSFetchedResultsController.deleteCache(withName: fetchedResultsController.cacheName)