gpt4 book ai didi

ios - RestKit:托管对象映射到一个普通的旧 Objective-C 对象中

转载 作者:行者123 更新时间:2023-11-29 12:45:59 25 4
gpt4 key购买 nike

Preamble: I'm using RestKit 0.21.0, but I've also tried 0.23.0.

我有许多映射,其中一个普通的旧 Objective-C 对象(我们称之为 POOCO)包含一个 NSManagedObject。似乎在这个根 POOCO 上执行 GET 请求时,托管对象是在 mainQueueManagedObjectContext 下创建的。

但是,当我对 POOCO 执行 PUT 时,加载响应时(响应是已发送对象的副本),托 pipe 对象是在临时 NSManagedObjectContext 下创建的,其父对象是 mainQueueManagedObjectContext

关于在 RestKit 中重新获取的快速旁白:

If the request were composed entirely of NSManagedObject instances, the resulting objects would be "refetched" using (I think) the mainQueueManagedObjectContext. (IIRC, once all the object mapping is complete, the objects and sub-objects would all be re-fetched from the main MOC, replacing the objects that were created on the temporary MOC used for mapping.)

This refetching is done (as far as I can tell) because when a MOC gets dealloc'd, all of the managed objects that it manages become invalid. So we refetch the objects in a MOC to which myObjectManager.managedObjectStore maintains a strong reference, so that they remain valid after the temporary MOC goes away.

因为映射中的根对象是一个 POOCO,它对托管对象的任何引用都不会被重新获取,所以它们仍然附加到临时 MOC,并且一旦映射完成就变得无效。

有没有人遇到过这样的问题?您是否可以建议最佳实践?是否可以告诉 RestKit,“使用 mainQueueManagedObjectContext 重新获取这些托管对象”或类似的东西?

最佳答案

托管对象实例不能在线程间传递,RestKit在后台线程上做映射。如果返回的对象是托管对象,那么它们将被转移到主线程。可以争辩说,嵌套托管对象没有为您切换到主线程是 RestKit 中的错误/疏忽(您可以在 github 中提出问题)。

您可以要求 RestKit 使用主线程来做一些事情(通过显式创建操作并运行它们,尽管这并没有涵盖所有内容)但您真的不想那样做。

所以,您的主要选择是在您的容器对象中有一个方法,您可以在 success block 中调用该方法,该方法遍历包含的托管对象并“重新获取”它们 - 如您所知所有对象都存在这可以通过 objectWithID: 完成,并且在主线程上调用 success block 。

关于ios - RestKit:托管对象映射到一个普通的旧 Objective-C 对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23688670/

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