更新 NSManagedObject 会导致 NSFetchedResultsController 删除

发布于 2024-11-03 02:30:34 字数 557 浏览 4 评论 0原文

我有一个相当令人沮丧的问题,我已经困扰了很长一段时间。

为了提供一些上下文和细节,我有一个 iOS UISplitViewController 应用程序 - 标准主/细节内容。主视图是一个由 NSFetchedResultsController 支持的 UITableView(它从 SQLite 数据存储加载 NSManagedObjects)。

似乎正在发生的情况是,详细信息视图中的任何更新(通常会导致“主记录”更新并刷新到 NSManagedObject 并最终刷新到 SQL 数据存储)会导致 NSFetchedResultsController 上的 DELETE 操作。

我认为这是因为对 NSManagedObject 属性的写入导致了某种错误,这反过来又导致 NSFetchedResultsController 将其从缓存的结果集中删除。最终结果是记录从主视图中“丢失”(例如:UITableCellView 的记录从主 UITableView 中删除)。

问题是我不希望这种情况发生,而且我不知道如何阻止它......

有人以前经历过这个问题并且可以提供一些指导吗?

提前致谢, 本

I have quite a frustrating problem that I have been struggling with for quite some time.

To provide some context and detail I have an iOS UISplitViewController application - standard master / detail stuff. The master view is a UITableView backed with an NSFetchedResultsController (which loads NSManagedObjects from a SQLite data store).

What seems to be happening is that any update within the details view (which can routinely cause updates to the 'master records' and are flushed to NSManagedObject's and ultimately the SQL data store) causes a DELETE operation on the NSFetchedResultsController.

I assume that this is because the write to the NSManagedObject property(s) are causing a fault of some kind, which in turn causes the NSFetchedResultsController to expunge it from it's cached result set. The end result is that records go 'missing' from the master view (e.g.: UITableCellView's are removed from the master UITableView).

The issue is that I don't want this to happen and I have no idea how to stop it...

Has anyone experienced this issue before and could possibly provide some guidance?

Thanks in advance,
Ben

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

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

发布评论

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

评论(1

甜扑 2024-11-10 02:30:34

我不确定这是否能回答您的问题,但我找到了解决我遇到的类似问题的方法。我按照每个对象名称的首字母对核心数据支持的 UITableView 中的对象进行排序。在我阅读的任何教程中,它都告诉我在 NSManagedObject 子类中放置一个瞬态属性“NameInitial”,我将用该对象名称的第一个字母填充该属性。然后,我使用该属性作为我的sectionNameKeyPath,将对象排序到 UITableView 中的正确部分。

我在每个单元格上都有一个按钮,用于更新与该单元格关联的对象的属性,并且我在 didChangeObject 委托函数中正确收到了 NSFetchedResultsChangeUpdate 消息。然而,有时,单元格会被删除,并且我会无缘无故地收到 NSFetchedResultsChangeDelete 消息。

然后我注意到被删除的单元格的关联对象的 NameInitial 属性为 (null)。我忘记了瞬态 NameInitial 仅存储在内存中,因此不一定一直维护。每次更新单元格时手动重新填充 NameInitial 属性后,删除就会停止。因此,如果您使用瞬态属性来帮助对 UITableView 进行排序/分段,这可能是您的问题。

希望这有帮助,祝你好运!
-瑞克

I'm not sure if this answers your question, but I figured out the solution to a similar problem I was having. I was sorting the objects in my Core Data-backed UITableView by the first letter of the name of each object. In whatever tutorial I read, it told me to put a transient property 'NameInitial' in the NSManagedObject subclass that I would populate with the first letter of the name of that object. I then used that property as my sectionNameKeyPath to sort the objects into the proper sections in my UITableView.

I had a button on each cell that updated a property of the object associated with that cell, and I properly received NSFetchedResultsChangeUpdate messages in my didChangeObject delegate function. HOWEVER, sometimes, cells would get deleted and I would receive the NSFetchedResultsChangeDelete message for no apparent reason.

Then I noticed that the cells that were getting deleted had (null) as the NameInitial property for their associated object. I had forgotten that the transient NameInitial is only stored in memory, and so is not necessarily maintained all the time. Once I manually repopulated the NameInitial property each time I updated a cell, the deleting stopped. So if you are using a transient property to help sort/section your UITableView, this might be your problem.

Hope this helps, and good luck!
-Rick

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