RestKit一对多关系逆映射

时间:2015-09-08 11:51:04

标签: ios restkit restkit-0.20

我使用RestKit将JSON API映射到iOS对象

+ (RKObjectMapping *)addressMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Address class]];

    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"id": @"idAddress",
                                                  @"id_city": @"idCity",
                                                  @"id_country": @"idCountry"
                                                  }];

    return mapping;
}

+ (RKObjectMapping *)userMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]];

    [mapping addAttributeMappingsFromDictionary:@{
                                                  @"id": @"idUser",
                                                  @"email": @"email",
                                                  }];

    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"address" toKeyPath:@"address" withMapping:[self addressMapping]]];
    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"branch_addresses" toKeyPath:@"branchAddresses" withMapping:[self addressMapping]]];

    return mapping;
}

当我从API获取数据时,它们已正确映射到类:

RKResponseDescriptor *responseDescriptorGet =
    [RKResponseDescriptor responseDescriptorWithMapping:[RestMappingProvider userMapping]
                                                 method:RKRequestMethodGET
                                            pathPattern:@"/api/user/user"
                                                keyPath:@"data"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

但是,当我更新数据时,一对多关系(branch_addresses)未正确映射:

RKRequestDescriptor *requestDescriptor =
    [RKRequestDescriptor requestDescriptorWithMapping:[[RestMappingProvider userMapping] inverseMapping]
                                          objectClass:[User class]
                                          rootKeyPath:nil
                                               method:RKRequestMethodPUT];

服务器收到的数据:

[id] => 123
[email] => test@test.com
[address] => Array
    (
        [id] => 3
        [id_city] => 1
        [id_country] => 1
    )
[branch_addresses] => Array
    (
        [0] => Array
            (
                [id] => 4
            )

        [1] => Array
            (
                [id_city] => 1
            )

        [2] => Array
            (
                [id_country] => 1
            )
    )

服务器上的数据应该是什么样的:

[id] => 123
[email] => test@test.com
[address] => Array
    (
        [id] => 3
        [id_city] => 1
        [id_country] => 1
    )
[branch_addresses] => Array
    (
        [0] => Array
            (
            [id] => 4
            [id_city] => 1
            [id_country] => 1
            )
    )

服务器端的php://输入内容(原始数据):

id=123&email=test%40test.com&address[id]=3&address[id_city]=1&address[id_country]=1&branch_addresses[][id]=4&branch_addresses[][id_city]=1&branch_addresses[][id_country]=2&branch_addresses[][id]=6&branch_addresses[][id_city]=1&branch_addresses[][id_country]=1

1 个答案:

答案 0 :(得分:1)

并非所有形式的数据传输都具有同等表现力。您似乎在发送查询参数而不是JSON,或者至少是表单编码的数据集。您需要设置默认请求mime类型,以便restkit知道您要发送JSON并将其转换为所需的格式。