我不知道如何设置这种关系映射:
@interface Account : NSManagedObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber accountID;
@property (strong, nonatomic) NSSet *sessions;
@end
.
@interface Session : NSManagedObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSNumber sessionID;
@end
JSON 我正在尝试映射...
{
"userSessions": {
"name" : "Mr. User",
"identification": 54321,
"sessions": [
"418",
"419",
"431",
"441",
"457",
"486",
"504"
]
}
}
Sessions 和 Account 已经是从其他两个 API 调用(getSessions 和 getAccountInfo)正确映射的对象。我很难让两者之间的关系正常工作。数字数组表示sessionID
s。
这是我目前尝试过的(它崩溃了):
RKEntityMapping *sessionRelationship = [[RKEntityMapping alloc] initWithEntity:sessionEntity];
[sessionRelationship setIdentificationAttributes:@[ @"sessionID" ]];
[sessionRelationship addAttributeMappingFromKeyOfRepresentationToAttribute:@"sessionID"];
RKEntityMapping *accountMapping = [[RKEntityMapping alloc] initWithEntity:accountEntity];
[entityMapping setIdentificationAttributes:@[ @"accountID" ]];
[entityMapping addRelationshipMappingWithSourceKeyPath:@"sessions"
mapping:sessionRelationship];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping
pathPattern:nil
keyPath:@"userSessions"
statusCodes:statusCodes];
编辑:
按照 Blake 的建议,我使用了 nil 的 sourceKeyPath,但是我仍然遇到了崩溃。
我沿着兔子洞追踪它,坠机发生在
RKValueForAttributeMappingInRepresentation(RKAttributeMapping *, NSDictionary *)
...这是因为字典参数实际上是 JSON 数组中的 NSString。
(lldb) po attributeMapping
(RKAttributeMapping *) $26 = 0x076c6a00 <RKAttributeMapping: 0x76c6a00 (null) => rec>
(lldb) po representation
(NSDictionary *) $27 = 0x0e4a20c0 431
(lldb) po [representation class]
(id) $28 = 0x01f9b8e4 __NSCFString
堆栈跟踪可以在这里看到: