RestKit第二级对象映射

时间:2014-04-26 13:38:40

标签: ios objective-c mapping restkit restkit-0.20

我将尝试解释我希望如何完成映射:

TOP lvl json对象包含Players对象,它是Player个对象的数组 每个Player对象都包含一个数组,我希望数组中的每个对象都是Event对象(自定义对象)。

现在因为我有一个Player对象的映射,并且我正在填充数组,而不是Event个对象(这就是我想要的),我就是这样的获取NSDictionary个对象。事情是我确实有Event类的映射。我的问题是让 RestKit 将这些映射到数组中。 我尝试添加Event类的responseDescriptors,但我没有运气。

这是Player对象映射

 RKObjectMapping* playerMapping = [RKObjectMapping mappingForClass:[Player class]];
    [playerMapping addAttributeMappingsFromDictionary:@{
                                        ...more here              
                                           @"activeEvents" : @"activeEvents"
                                                         }];

这是请求方法

 NSURL *taskURL = [NSURL URLWithString:kAppWebApiURLPath];

    // Set object manager with base url
    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    objectManager = [RKObjectManager managerWithBaseURL:taskURL];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

    [objectManager.HTTPClient setDefaultHeader:@"Authorization" value:kAppWebAPIKey];
    [objectManager.HTTPClient setDefaultHeader:@"Content-Type" value:@"application/json"];

RKRequestDescriptor * requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[MappingProvider inverseLoginMapping]  objectClass:[LoginInfo class] rootKeyPath:nil method:RKRequestMethodPOST];

[objectManager addRequestDescriptor:requestDescriptor];

RKResponseDescriptor *playersResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider playerMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"players" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:playersResponseDescriptor];

NSLog(@"%@",loginInfo.iOSDeviceToken);
[objectManager postObject:loginInfo path:kAppWebApiLoginPath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {}...

更新,我现在需要更进一步的映射,我的播放器对象包含我使用

成功映射的事件数组
[playerMapping addRelationshipMappingWithSourceKeyPath:@"activeEvents" mapping:[MappingProvider eventMapping]];

现在每个Event对象都包含一个Players数组,因此它就像Players - >活动 - >播放器。

以下是Event和Player对象的映射:

RKObjectMapping* eventMapping = [RKObjectMapping mappingForClass:[Event class]];

    [eventMapping addAttributeMappingsFromDictionary:@{

              @"stuffhere" : @"stuffz"                              
                                                       }];


    RKObjectMapping* playerMapping = [RKObjectMapping mappingForClass:[Player class]];
    [playerMapping addAttributeMappingsFromDictionary:@{
                                                        @"name": @"name",
                                                        @"activeEvents" : @"activeEvents"
                                                        }];
    [eventMapping addRelationshipMappingWithSourceKeyPath:@"activeEvents/players" mapping:playerMapping];

现在我没有获得递归函数,但是如何在代码中声明建立这种关系 映射json数组以分配给我的本地数组属性?

1 个答案:

答案 0 :(得分:0)

从映射中删除@"activeEvents" : @"activeEvents"并将其替换为:

[playerMapping addRelationshipMappingWithSourceKeyPath:@"activeEvents" mapping:eventMapping];

您还应该只有一个响应描述符,因为数据是嵌套的。