1

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

@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)正确映射的对象。我很难让两者之间的关系正常工作。数字数组表示sessionIDs。

这是我目前尝试过的(它崩溃了):

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

堆栈跟踪可以在这里看到: Stack Trace

4

1 回答 1

2

You have stumbled across an unhandled case within RestKit's mapping logic. There was a hand-off between layers of the mapping engine that assumed that the representation being passed was an NSDictionary, but the caller was not enforcing its part of the contract. I have taken your example JSON and mapping structure and used it to build a unit test in RestKit.

This issue is fixed on the development branch as of https://github.com/RestKit/RestKit/commit/d761de0ea7cfb11851b714e49ff3126d65ab7b49 and will be included in the rc1 version I am going to tag this weekend.

于 2013-01-26T05:24:31.353 回答