0

我有两个实体“父母”和“孩子”。父实体与子实体具有一对多的关系,子实体与父实体具有一对一的关系,并且“父”实体具有我每次都映射的唯一 ID(主键属性)。

当我调用 API 时,响应是这样的

{
    "parent": {
        "ss": "1",
        "uid": 200,
        "me": "Successfully Retrieved",
        "pn": 2,
        "cl": [
            {
                "cid": 1500,
                "cn": "XYZ"
            },
            {
                "cid": 1501,
                "cn": "ABC"
            }
        ]
    }
}

上述响应成功映射到 DB。我可以通过返回两个对象的 [parent.childs allObjects] 访问。

我有一个加载更多的功能,它调用相同的 api 和页码递增 1,我得到类似于上述一个的响应(3 个对象)。但是当我尝试从数据库中获取数据时,我只得到了最新响应的 3 个对象。我之前映射的两个对象的关系变为空。所以我无法访问所有对象。我如何通过关系访问数据库中的所有数据。

这就是我映射的方式

RKObjectManager *manager = [[RestKit sharedDataManager] objectManager];
        RKEntityMapping *parentMapping = [[MFResponseMapper sharedInstance]parentMapper]; //Primary key is mapping here
        RKEntityMapping *childMapping = [[MFResponseMapper sharedInstance]childMapper]; //Child name and Id is mapping here.

        [parentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"cl"
                                                                                    toKeyPath:@"childs"
                                                                                  withMapping:childMapping]];


        RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:parentMapping
                                                                                       pathPattern:nil
                                                                                           keyPath:@"parent"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

        [manager addResponseDescriptor:responseDescriptor];

注意:我在模型中有删除父子(一对多)关系的规则。

谢谢

4

2 回答 2

1

在关系映射上,将 设置assignmentPolicyRKUnionAssignmentPolicy

于 2013-09-18T07:01:47.973 回答
0

你应该添加

parentMapping.identificationAttributes = @[@"uid"];

childMapping.identificationAttributes = @[@"cid"]; 

您必须这样做的原因是因为否则它只会创建另一个具有 3 个孩子的父母,这就是您所看到的。因此,基本上在您再次调用 API 之后,您将从 2 个 API 调用中获得 2 个父级以及相应的子级(两个和三个)。

于 2013-09-18T07:00:08.003 回答