RestKit - 关系映射

时间:2013-01-26 01:44:49

标签: restkit

我不知道如何设置这种关系映射:

@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"
        ]
    }
}

会话和帐户已经从其他两个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 *)

...这是因为dictionary参数实际上是来自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

可以在此处看到堆栈跟踪: Stack Trace

1 个答案:

答案 0 :(得分:2)

你在RestKit的映射逻辑中偶然发现了一个未处理的案例。在映射引擎的各层之间进行了一次切换,假设传递的表示是NSDictionary,但调用者没有强制执行其合同的一部分。我已经使用了您的示例JSON和映射结构,并使用它在RestKit中构建单元测试。

此问题已在https://github.com/RestKit/RestKit/commit/d761de0ea7cfb11851b714e49ff3126d65ab7b49开发分支上修复,并将包含在本周末我要标记的rc1版本中。