ios - RestKit 一对多关系逆映射

标签 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://input的内容(原始数据):

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 并将其转换为所需的格式。

关于ios - RestKit 一对多关系逆映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457087/

相关文章:

iphone - 安装Ad Hoc应用程序进行测试而不同步iTunes

ios - RestKit崩溃— RKManagedObjectRequestOperation.m第873行

ios - 使用休息套件第二次请求后响应不正确

iphone - 使用 RestKit 0.20.x 下载和解压 zip 文件

ios - 休息套件 : Not able to perform mapping using coredata

iOS Storyboard中的 View Controller 被分组到另一个 Storyboard中

iphone - NSString 错误

ios - 无法从 Dark Sky API 获取数据

objective-c - 使用 Restkit 对象映射的同一对象的多个实例

ios - 使用 RestKit 映射我的对象