我对 NSManagedObjectContext 依赖注入做错了什么?

发布于 2024-09-19 12:59:47 字数 624 浏览 5 评论 0原文

我正在尝试按照 Marcus Zarra 的建议使用 NSManagedObjectContext 依赖注入 - - 我正在我的 AppDelegate 中创建一个 MOC,并将其作为保留属性传递给我的每个视图控制器。

一般来说,这似乎工作得很好,但在通过 NSFetchedResultsController 呈现数据的模态表视图控制器中,我只能看到应用程序启动时数据库中的内容。也就是说,如果用户在运行时添加数据,它会正确添加到数据库中,但在打开模式 ViewController 并创建 NSFetchedResultsController (使用注入的 NSManagedObjectContext)时不会出现。但是,如果我关闭应用程序并重新启动,然后打开模式视图控制器,我确实会看到在上一个会话中添加的数据。

在创建 NSFetchedResultsController 之前我是否必须以某种方式刷新 MOC?我绝对确定在输入新的用户数据之后,正在创建模态视图控制器和 NSFetchedResultsController,并且正在执行获取。

I am trying to use NSManagedObjectContext dependency injection as recommended by Marcus Zarra -- I'm creating an M.O.C. in my AppDelegate and passing it as a retained property to each of my view controllers.

Generally this seems to work well, but in a modal table view controller that presents data via an NSFetchedResultsController, I only see what was in the database when the app was launched. That is, if the user adds data at runtime, it gets added correctly to the database, but does not appear when the modal ViewController is opened and the NSFetchedResultsController is created (using the injected NSManagedObjectContext). However, if I close the app and restart, then open the modal view controller, I do see the data added in the previous session.

Do I have to refresh the M.O.C. in some way prior to creating the NSFetchedResultsController? I am absolutely sure that the modal view controller and the NSFetchedResultsController are being created, and the fetch is being executed, AFTER the new user data has been entered.

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

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

发布评论

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

评论(1

闻呓 2024-09-26 12:59:47

首先,您应该在应用程序委托和视图控制器中记录 moc,以确认两个地方的 moc 具有相同的地址,因此是相同的对象。

如果是,那么很可能 FRC 缓存出现问题。将缓存设置为零和/或刷新缓存并查看是否可以解决该问题。

To start, you should log the moc in both app delegate and your view controller to confirm that the moc in both places has the same address and is therefore the same object.

If it is, then most likely you've got an issue with the FRC's cache. Set the cache to nil and/or refresh the cache and see if that resolves it.

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