给出3种实体类型:User
(会话参与者),Chat
(2 User
s之间的文字对话)和VideoCall
(始终与Chat
相关联{1}},其中一个用户是呼叫者)。
如果API端点返回VideoCall
对象,则映射到Chat
和User
的外键可以正常工作。
<DRVideoCall: 0x16f27f00> (entity: DRVideoCall; id: 0x182a1c90 <x-coredata://07D2A791-2CE9-46B1-8060-E288EC2ED4A4/DRVideoCall/p9> ; data: {
caller = "0x16fbbff0 <x-coredata://07D2A791-2CE9-46B1-8060-E288EC2ED4A4/DRDoctor/p1>";
callerId = 307;
chat = "0x1826dca0 <x-coredata://07D2A791-2CE9-46B1-8060-E288EC2ED4A4/DRChat/p185>";
chatId = 2748;
state = calling;
})
但如果它作为关系嵌套到另一个映射中,则不会,chat
和caller
属性为nil
。
<DRVideoCall: 0x191bebb0> (entity: DRVideoCall; id: 0x1918b3d0 <x-coredata:///DRVideoCall/t24460E4B-9B6B-43F3-9981-62FDE53BFD5919> ; data: {
caller = nil;
callerId = 307;
chat = nil;
chatId = 2761;
state = calling;
})
RestKit是否可以使外键关系适用于嵌套对象?
事件映射
RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"DREvent"
inManagedObjectStore:managedObjectStore];
[eventMapping addAttributeMappingsFromDictionary:@{ @"type" : @"type" }];
RKRelationshipMapping *eventCallMapping =
[RKRelationshipMapping relationshipMappingFromKeyPath:@"call"
toKeyPath:@"call"
withMapping:[DRVideoCall mappingInManagedObjectStore:managedObjectStore]];
[eventMapping addPropertyMapping:eventCallMapping];
视频通话映射
RKEntityMapping *videoMapping = [RKEntityMapping mappingForEntityForName:@"DRVideoCall"
inManagedObjectStore:store];
[videoMapping addAttributeMappingsFromDictionary:@{ @"calling_user_id" : @"callerId",
@"chat_id" : @"chatId" }];
[videoMapping addConnectionForRelationship:@"chat" connectedBy:@{ @"chatId" : @"chatId" }];
[videoMapping addConnectionForRelationship:@"caller" connectedBy:@{ @"callerId" : @"userId" }];
UPD:以下是VideoCall
JSON的样子
{
"chat_id":2748,
"calling_user_id":307,
"state":"calling",
"session_id":"...",
"token":"..."
}
Event
JSON:
{
"call":{
"calling_user_id":307,
"chat_id":274,
"session_id" = "...",
"state":"calling",
"token":"..."
},
"type":"call"
}