Rails 3.1 和 Rails 3.1 RestKit 0.9.3 - 引用映射
我有以下场景: 我有一个 Rails Web 门户 (Rails 3.1),我使用 RestKit 0.9.3(最新版本)从 iOS 设备连接该门户。 我想将服务器的数据结构映射到iOS设备上的核心数据模型。该模型的设计如下:
类
- 用户
- 角色
- 帖子
- 评论
关系
- 一个用户有零个或一个角色/一个角色可以有多个用户
- 一个用户有多个帖子/一个帖子有一个用户
- 一篇文章有多个评论/一篇评论有一篇文章
问题是,RestKit 中关系的默认映射会导致大量消息和冗余传输,因为当我发送/接收角色时,所有用户也将被传输,并且如果我发送/接收用户,角色被添加到每个用户。用户、帖子和评论也会发生同样的情况。 数据太多,据我所知,最好的解决方案是只发送 ID,这些 ID 会被引用并传输每种类型。 当我通过将角色的 ID 映射到用户中的“role_id”字段来将数据从 iOS 设备发送到服务器时,这非常有效,因为 Rails 确实知道如何处理它。因此,关系不会映射为关系,而只是映射为 ID 字段。但是,当我从服务器接收用户(设备上不存在该用户)时,我收到错误,因为 RestKit 不明白如何处理映射,因为没有链接到用户对象的角色对象。
所以我研究发现可以映射关系并使用主键的映射,这样RestKit就只发送角色的主键。但现在 Rails 不明白如何处理它。 它看起来像这样:
{ “id”:1, “uid”:“我的用户名”, "pwd":"我的密码", “角色”: { “id”:0 } 但
Rails 期待这样的事情:
{ “id”:1, “uid”:“我的用户名”; "pwd":"我的密码", “角色 ID”:0 这
就是我现在的状态,我真的不知道如何解决这个误解。
我将非常感谢任何帮助!
I have the following scenario:
I have a rails web portal (Rails 3.1) and I use RestKit 0.9.3 (the newest version) to connect the the portal from an iOS device.
I want to map the data structure from the server to the core data model on the iOS device. The model is designed as follows:
Classes
- User
- Role
- Post
- Comment
Relations
- A User has zero or one Role / A Role can have multiple Users
- A User has multiple Posts / A Post has one User
- A Post has multiple Comments / A Comment has one Post
The problem is, that the default mapping of relations in RestKit results in huge messages and redundant transfer because when I send/recieve the Roles, all the Users will be transfered as well and if I send/recieve the Users, the Role is added to each User. The same occurs with Users, Posts and Comments.
It's just to much data and the best solution, as far as I could think of one, is to just send the IDs, which are referenced and transfer each type.
This works perfectly when I send the data from the iOS device to the server by mapping the IDs of the Role to a field "role_id" in the User, because Rails does understand what to do with that. So the relations are not mapped as relations but just as ID fields. But when I receive Users from the server, which where not present on the device, I get an error, because RestKit does not understand what to do with the mapping, since there is no Role object linked to the User object.
So I researched and found out that you can map the relations and use a mapping of the primary key, so that RestKit would only send the Primary Key of the Role. But now Rails does not understand what to do with it.
It looks something like this:
{
"id":1,
"uid":"my_user_name",
"pwd":"my_password",
"role": {
"id":0
}
}
But Rails is expecting something like that:
{
"id":1,
"uid":"my_user_name";
"pwd":"my_password",
"role_id":0
}
That's my current state and I really don't know how to solve this misunderstandings.
I would be very thankful for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种可能的解决方案:
1) 让您的 Rails 模型意识到您希望通过添加
或
2) 更改客户端代码的序列化映射来批量更新角色关系。
There are two possible solutions for that:
1) Make your rails model aware of the fact that you want to mass update the role-relationship by adding
or
2) Change the serialization mapping of your client code.