RestKit 一对多关系逆映射

RestKit one to many relationship inverse mapping

我正在使用 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

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