MagicalRecords从JSON / NSDictionary导入数据。导入关系 [英] MagicalRecords import data from JSON / NSDictionary. Import relationships

查看:183
本文介绍了MagicalRecords从JSON / NSDictionary导入数据。导入关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关系的问题。我有NSManagedObject类Team和Player。团队可以包含许多玩家。

I have questions with relationships. I have to NSManagedObject classes Team and Player. Team can contain many players. Player can contain only one team.

以下是两个请求的JSON:

Here is the JSON from the two requests:

小组

    {
        "id" : 1,
        "name" : "Chicago Bulls",
        "city" : "Chicago"
    },
    {
        "id" : 2,
        "name" : "Detroit Pistons",
        "city" : "Detroit"
    },
...

/ players

{
    "id" : 1,
    "name" : "D.J. Augustin",
    "teamId" : 1
},
{
    "id" : 2,
    "name" : "Carlos Boozer",
    "teamId" : 1
},
...

设置xcdatamodeld实体

Setup xcdatamodeld entities

Team Entity

atribute | mappedKeyName |
name     | name          |
teamID   | id            |
city     | city          |


Player Entity

atribute | mappedKeyName |
name     | name          |
playerID | id            |

正如你所看到的,我没有为Player实体设置teamId,因为我们需要使用relatedByAttribute 。这里的第一个问题我不知道为什么属性,属性或关系我需要在User Info中设置relatedByAttribute键。

As you can see I have no set teamId there for Player entity, because we need to use relatedByAttribute key. First problem here I don't know for which property, attribute or a relationship I need to set relatedByAttribute key in User Info.

WRITE CODE PART

假设我已经得到了由NSDictionary代表的团队JSON。我所做的:

Suppose I already got team JSON represented by NSDictionary. What I do:

此代码使用用户信息为NSDictionary导入Team实体的每个属性的所有数据。在这一步,一切都很好。

This code blow import all data from NSDictionary using User Info for each attribute for the Team entity. On this step everything is going fine.

Team *team = [Team MR_createEntity];
[team MR_importValuesForKeysWithObject:dictionary];

第二个操作是获取所有玩家并将它们附加到已经在第一步创建的具体团队使用您可以在JSON中看到的关键teamId。

The second operation is get all players and attach them to the concrete team that already created on the first step using key teamId that you can see in JSON.

我在此步骤中执行的操作。假设我有第一个播放器的NSDictionary。

What I do on this step. Let suppose I got NSDictionary with first player.

代码是一样的。

Player *player = [Player MR_createEntity];
[player MR_importValuesForKeysWithObject:dictionary];

但是关系将无法工作,MagicalRecords不会把我的播放器连接到具体的团队,因为我没有设置relatedByAttribute键如我所说。我真的需要帮助这个,因为我理解导入的基本想法,但不能理解我需要设置relatedByAttribute键我的意思是对于哪个实体和属性。

But the relationships will no work, and MagicalRecords will no connect my player with the concrete team, because I have not set relatedByAttribute key as I said. I really need to help with this, because I understand the basic idea of import but can't understand where I need to set relatedByAttribute key I mean for which entity and for which attribute.

我还看到了一些重复的实体问题,以及这里的堆栈溢出,我认为这是另一个问题,但如果你可以建议的东西,这将是好的,因为我从头开始,可以使我的代码容易理解,没有其他问题。非常感谢!

I saw also some duplicated issues with entities as well here on stack overflow, I think it is another question, but if you can suggest something it will be good, because I start from scratch and can make my code easy for understanding and without additional issues. Thank you a lot!

我使用的链接: magical import

推荐答案

是关系的用户信息的mappedKeyName。

I solved this problem one thing I miss was mappedKeyName for the Relationship's user info.

首先,我们需要为与JSON键相对应的关系设置mappedKeyName。

At first we need to set mappedKeyName for the relationship that corresponds to the JSON key.

所以,如果JSON你看起来像下面:

So in case if JSON you got looks like below:

{
    "status": 1,
    "message": "success",
    "player": {
        "playerID": 1,
        "teamID": 14,
        "firstname": "NAME",
    }
}

mappedKeyName 键将是player.teamID

mappedKeyName key will be player.teamID

relatedByAttribute 键将是TeamID,这是Team实体的属性。

relatedByAttribute key will be teamID that is an attribute of the Team entity.

希望它能帮助别人。

这篇关于MagicalRecords从JSON / NSDictionary导入数据。导入关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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