NSFetchedResultsController 委托方法在 mergeChangesFromContextDidSaveNotification 后未触发
浏览了与此类似的帖子,但没有一个回答我的问题。
与 CoreDataBooks 一样,我使用单独的 MOC 来添加和编辑项目并观察正确的通知,如下所示:
- (void)addControllerContextDidSave:(NSNotification*)saveNotification {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
[context mergeChangesFromContextDidSaveNotification:saveNotification];
NSLog(@"merging changes in rootview");
}
当我进行保存时,会正确观察到它。调试通知会显示正确的上下文,调试上下文会显示刚刚添加的项目。日志消息也打印得很好。但什么也没发生。 NSFetchedResultsController 的委托方法都不会触发,表视图也不会更新。
我尝试不缓存或清除它。我已经在 tableView 上尝试过 reloadData
,但没有任何结果。 当我退出应用程序并重新启动时,它显示得很好。
更新: 通知确实包含正确的上下文作为其 object
,其中包含正确的项目,但 userInfo
哈希中没有任何内容:
userInfo = {
inserted = "{(\n)}";
updated = "{(\n)}";
}
所以看起来通知没有尽管更改的项目具有正确的上下文,但其中有任何更改......嗯。
Been browsing through the posts similar to this one but none answered my problem.
Like CoreDataBooks, I use a separate MOC for adding and editing items and observe the proper notifications, like so:
- (void)addControllerContextDidSave:(NSNotification*)saveNotification {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
[context mergeChangesFromContextDidSaveNotification:saveNotification];
NSLog(@"merging changes in rootview");
}
When I do a save, it's observed correctly. Debugging the notification shows the right context, and debugging the context shows the just added item. The log message also prints just fine. But nothing happens. None of the delegate methods of the NSFetchedResultsController fire, nor is the table view updated.
I tried no cache or clearing it. I've tried reloadData
on the tableView, but nothing.
When I quit the app and restart, it shows up fine.
UPDATE:
The notification does indeed contain the right context as its object
, which contains the correct item, but the userInfo
hash has nothing in it:
userInfo = {
inserted = "{(\n)}";
updated = "{(\n)}";
}
So it looks like the notification doesn't have any changes in it, despite having the correct context with the changed item.... hmmmm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最愚蠢错误奖也太……克里斯托夫!!
通知没有显示任何内容的原因是我忘记在调用设置通知的委托然后再次保存之前在单独的 MOC 上进行保存。
由于 MOC 刚刚保存,第二次保存没有更改,因此通知保持为空。删除第一个保存,正如我应该更早做的那样,确实解决了问题。
And the award for dumbest mistake goes toooooo ... Christoph!!
The reason the notification was not showing anything was that I had forgotten to take out a save on the separate MOC before calling the delegate that set up the notification and then saved again.
Since the MOC was just saved, the second save had no changes and thus the notification stayed empty. Removing the first save, as I should have done much earlier, did fix the problem.