使用Restkit为Core数据创建Temp对象的更好方法 [英] Better Approach for Creating Temp Object for Core Data with Restkit

查看:101
本文介绍了使用Restkit为Core数据创建Temp对象的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我有这种情况下,我需要发布一个对象到远程服务器,并获得一个对象键,然后本地存储对象。我有我的应用程序中实现的Core数据和Restkit。

In my app, I have this scenario where I need to post an object to remoter server and get an object key back and then store the object locally. I have Core data and Restkit implemented in my app.

对象值是从用户输入收集的。我无法想出一个伟大的方式准备对象,然后发布到远程服务器。这个对象是NSManagedObject类型的实体,我不想在服务器获取对象ID之前存储它。

The object value are collected from user input. I couldn't figure out a great way to prepare the object before posting it to remote server. This object is an entity of type NSManagedObject, and I don't want to store it before I get the object id from server.

我遇到了建议使用瞬态对象来处理这种情况。但是正如在该线程中所讨论的,这导致代码维护的问题。

I came across this which suggested to use a transient object to handle this situation. But as discussed in that thread, this causes issue with code maintenance.

有更好的方法来处理这种情况吗?感谢。

Is there a better way to handle this scenario? Thanks.

推荐答案

使您的核心数据模型类遵守RKRequestSerializable协议。

Make your core data model class adhere to the RKRequestSerializable protocol.

然后当用户输入验证时,创建一个实体作为正常,并将其设置为params值到RKRequest,这将发送您的对象作为HTTP主体。看看RKParams.m里面的例子。

Then when the user input is validated, create an entity as normal and set it as the params value to the RKRequest, this will send your object as the HTTP body. Look inside RKParams.m for an example.

还将新创建的实体设置为RKObjectLoader的targetObject。这样,当您的Web服务返回信息(如新的唯一ID)时,它将定位新对象,并将新的唯一ID保存到此对象,而不创建重复。

Also set the newly created entity as the targetObject for the RKObjectLoader. That way, when your web service returns the information (like the new unique ID), it will target the new object and save the new unique ID to this object without creating a duplicate.

清除为泥?

PS:哦,小心地将自动生成的核心数据类与自定义代码混合!我建议您 mogen 帮助您在每次进行更改时不丢失代码。

PS: Oh and be careful mixing autogenerated core data classes with custom code! I recommend mogen to help you not lose code each time you make a change.

这篇关于使用Restkit为Core数据创建Temp对象的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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