「親」と「子」の 2 つのエンティティがあります。親エンティティは子エンティティと一対多の関係を持ち、子は親と一対一の関係を持ち、「親」エンティティには毎回マッピングする一意の ID (主キー属性) があります。
APIを呼び出すと、次のような応答が返ってきます
{
"parent": {
"ss": "1",
"uid": 200,
"me": "Successfully Retrieved",
"pn": 2,
"cl": [
{
"cid": 1500,
"cn": "XYZ"
},
{
"cid": 1501,
"cn": "ABC"
}
]
}
}
上記の応答は、DB に正常にマップされます。2 つのオブジェクトを返す [parent.childs allObjects] でアクセスできます。
同じ API を呼び出し、ページ番号を 1 ずつ増やしてロードする機能があり、上記のような応答 (3 つのオブジェクト) を取得します。しかし、DB からデータを取得しようとすると、最新の応答で 3 つのオブジェクトしか取得できません。以前にマップした 2 つのオブジェクトの関係が空になります。そのため、すべてのオブジェクトにアクセスできません。リレーションシップによって DB からすべてのデータにアクセスするにはどうすればよいですか。
これが私がマッピングしている方法です
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];
注: モデルの親子 (1 対多) 関係の削除ルールがあります。
ありがとう