我有两个实体“父母”和“孩子”。父实体与子实体具有一对多的关系,子实体与父实体具有一对一的关系,并且“父”实体具有我每次都映射的唯一 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];
注意:我在模型中有删除父子(一对多)关系的规则。
谢谢