带有谓词的 NSFetchedResultsController 会忽略从不同 NSManagedObjectContext 合并的更改
我使用 NSFetchedResultsController
呈现表视图内容,它有一个谓词:
[NSPredicate predicateWithFormat:@"visible == %@", [NSNumber numberWithBool:YES]]
在使用单独的 NSManagedObjectContext
的后台线程上,我更新了一些实体并更改了它们的 visible
值从 NO
到 YES
。保存并合并主线程的 NSManagedObjectContext 中的更改。但是 NSFetchedResultsController 的 fetchedObjects 并没有改变。此外,控制器不会在委托上调用 -controller:didChangeObject:...
。如果实体以相同的方式在主线程上更新(我的测试应用程序调用相同的方法),则一切都会按预期进行。
此外,Notification 的 NSUpdatedObjectsKey 也包含这些对象。
目前我找到的唯一解决方案是调用每个 NSUpdatedObjectsKey
实体:
NSManagedObjectContext *context = ... // main thread context
[context existingObjectWithID:[object objectID] error:nil]
此问题仅适用于之前与谓词不匹配的更新对象。
我错过了一些明显的东西吗?
I am presenting table view contents using NSFetchedResultsController
which has a predicate:
[NSPredicate predicateWithFormat:@"visible == %@", [NSNumber numberWithBool:YES]]
On background thread using separate NSManagedObjectContext
I update few of the entities and change theirs visible
value from NO
to YES
. Save, merge changes in main thread's NSManagedObjectContext
. But NSFetchedResultsController
's fetchedObjects
doesn't change. Also controller doesn't call -controller:didChangeObject:...
on delegate. If entities are updated on main thread in identical manner (my test app calls the same method), everything works as expected.
Also Notification's NSUpdatedObjectsKey
contains those objects.
Currently the only solutions I've found is to call for each of NSUpdatedObjectsKey
entities:
NSManagedObjectContext *context = ... // main thread context
[context existingObjectWithID:[object objectID] error:nil]
This issue is only with updated objects which previously didn't match the predicate.
Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果主
NSManagedObjectContext
没有为更新的对象触发NSManagedObjectContextObjectsDidChangeNotification
事件,因为它没有为错误的对象执行。通用修复(或跟踪需要此处理的对象 ID):
来自 NSManagedObject 类参考:
Turns out main
NSManagedObjectContext
didn' t event fireNSManagedObjectContextObjectsDidChangeNotification
for updated objects because it is not done for faulted objects.Generic fix (or keep a track of object IDs that needs this treatment):
From NSManagedObject Class Reference:
合并来自其他 NSManagedObjectContext 的更改后,您必须在 Background-NSManagedObjectContext 上调用 processPendingChanges 。
请参阅 CoreData 编程指南:
You have to call processPendingChanges on your Background-NSManagedObjectContext after you merged the changes from an other NSManagedObjectContext.
See CoreData Programming Guide: