RestKit:在纯粹的Objective-C对象中映射的托管对象 [英] RestKit: Managed object mapped inside a plain old Objective-C object

查看:161
本文介绍了RestKit:在纯粹的Objective-C对象中映射的托管对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


前言:我使用RestKit 0.21.0,但我也尝试了0.23.0。

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

我有一些映射,其中一个简单的老的Objective-C对象(我们称之为POOCO)包含一个 NSManagedObject 。看来,当对这个根POOCO执行GET请求时,托管对象在 mainQueueManagedObjectContext 下创建。

I have a number of mappings where a plain old Objective-C object (let's call that a POOCO) contains an NSManagedObject. It seems that when doing GET requests on this root POOCO, the managed object is created under the mainQueueManagedObjectContext.

当我对POOCO做一个PUT时,当响应被加载(响应是被发送的对象的副本)时,管理的子对象在临时 NSManagedObjectContext ,其父项是 mainQueueManagedObjectContext

However when I do a PUT against the POOCO, when the response is loaded (the response is a copy of the object that was sent), the managed sub-object is created under a temporary NSManagedObjectContext, whose parent is the mainQueueManagedObjectContext.

快速了解RestKit中的重新获取:

A quick aside on re-fetching in RestKit:


如果请求完全由 NSManagedObject 实例组成,则生成的对象将被refetched使用(我认为) mainQueueManagedObjectContext 。 (IIRC,一旦所有对象映射完成,对象和子对象都将从主MOC重新获取,替换在用于映射的临时MOC上创建的对象)。

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.)

这个refetching是完成的(就我所知),因为当一个MOC获取 dealloc 'd,其管理的所有托管对象无效。因此,我们在MOC中重取对象, myObjectManager.managedObjectStore 维护一个强引用,以便在临时MOC消失后它们仍然有效。

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,并在映射完成后变为无效。

Because the root object in the mapping is a POOCO, any references it has to managed objects do not get refetched, so they remain attached to the temporary MOC, and become invalid once mapping is complete.

有其他人遇到类似的问题吗?你有建议的最佳做法吗?是否可以告诉RestKit,使用 mainQueueManagedObjectContext 或类似的东西来重新获取这些托管对象?

Has anyone else run into issues like this? Are there best practices you can suggest? Is it possible to tell RestKit, "refetch these managed objects using the mainQueueManagedObjectContext" or anything like that?

推荐答案

问题实际上有一些关于RestKit执行重新获取的误解。在问题中描述的情况下,RestKit实际上 成功 RKRefetchingMappingResult c>块。调用该对象上的任何访问器(例如 array firstObject )实际上会执行重新获取。错误是我从来没有在 RKMappingResult 上调用任何这些方法。

The question actually has some misconceptions about when RestKit performs refetching. Under the circumstances described in the question, RestKit actually does return an instance of RKRefetchingMappingResult in the success block. Calling any of the accessors on that object (such as array or firstObject) actually does perform the refetching. The mistake was that I was never invoking any of these methods on the RKMappingResult.

我的假设是,我在PUT请求中发送的对象将成为操作的 targetObject ,该对象将通过RestKit就地更新。这就是为什么我从来没有觉得需要做任何事情与 RKMappingResult 。这个假设在技术上不是错误的,但是一旦映射完成而直接访问对象,而不是使用 RKMappingResult ,我就跳过了重写

My assumption was that, since the object I was sending in the PUT request would become the operation's targetObject, the object would be updated in-place by RestKit. This is why I never felt the need to do anything with the RKMappingResult. The assumption isn't technically wrong, but by accessing the object directly once the mapping was complete instead of using the RKMappingResult, I was skipping the refetching step.

我在问题我已向RestKit人员提交,以在某些情况下自动进行重试。

I've proposed a change in this behaviour in an issue I've filed with the RestKit folks, to make refetching automatic in certain circumstances.

当使用 RKManagedObjectRequestOperation (无论是手动指定还是RestKit为您选择此类)进行请求时,始终确保您 RKMappingResult 以确保结果从正确的 NSManagedObjectContext 重新获取。

When making a request using RKManagedObjectRequestOperation (whether you specify that manually or RestKit selects this class for you), always make sure you exercise the RKMappingResult to ensure that the results are re-fetched from the correct NSManagedObjectContext.

这篇关于RestKit:在纯粹的Objective-C对象中映射的托管对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆