gpt4 book ai didi

ios - 核心数据和 NSFetchedResultsController : how to retrieve a managed object

转载 作者:行者123 更新时间:2023-11-28 20:07:12 26 4
gpt4 key购买 nike

问题:

我正在阅读 Tim Roadley 的“Learning Core Data of iOS”。我不明白为什么作者在从 fetched results controller 中检索托管对象时采取额外的、看似多余的步骤。

NSManagedObjectID *itemId = [[self.fetchedResultsController objectAtIndex:indexPath] objectID];

// Item is a NSManagedObject subclass
Item *item = (Item *)[self.fetchedResultsController.managedObjectContext existingObjectWithID:itemId error:nil];

// gets and sets item's attributes…

这不奇怪吗?为什么要麻烦地使用 objectIDexistingObjectWithID:error: 方法,而不是仅仅使用获取结果 Controller 的 objectAtIndexPath: 方法来检索项目对象?

我为尝试理解所做的工作:

(1.) 我在整本书的电子版中搜索了 existingObjectWithID:error: 方法。整本书都在使用该方法,但我找不到对其用法的解释。

(2.) 我在 existingObjectWithID:error: 上阅读了 Apple 文档

If there is a managed object with the given ID already registered in the context, that object is returned directly; otherwise the corresponding object is faulted into the context. This method might perform I/O if the data is uncached. Unlike objectWithID:, this method never returns a fault.

对象可能“在上下文中出错”,但方法“从不返回错误”。这不是自相矛盾吗?我只想说文档没有帮助我解决这个问题。

(3.)想了一天,还是没有顿悟。然而,我怀疑答案与在后台保存有关,稍后将添加到本书的演示应用程序中。

最佳答案

这一步是不必要的。虽然约瑟夫解释了它的理由,但它并不真正适用。当对象被删除时,NSFetchedResultsController 将检测到删除并做出适当的 react 。

我怀疑 Joseph 是正确的,作者试图避免删除对象,但这不是正确的方法。如果您 NSFetchedResultsController 正在返回已删除的对象,或者正在从 NSFetchedResultsController 下删除对象,那么您的应用程序有其他可能与线程相关的问题。

只需使用-objectAtIndexPath:

更新

If you were fetching managed objects without a NSFetchedResultsController, would JosephH's justification hold?

他正在讨论的解决方案是一个解决实际问题的创可贴。

如果一个应用程序遇到对象被从其下面删除的情况,那么就有一个更大的问题需要解决。

它会起作用吗?是的。

是否正确答案,否。

关于ios - 核心数据和 NSFetchedResultsController : how to retrieve a managed object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com