RestKit:“multipartFormRequestWithObject”仅发送附件,对象属性丢失

时间:2014-03-15 06:32:32

标签: ios restkit

我需要使用图片发布 Realty 对象:

-(void) addRealty:(Realty*)realty withImages:(NSDictionary*)images success:(void (^)(Realty *))callback failure:(void(^)(void))failure
{
   UIImage *image = [UIImage imageNamed:@"test.png"];

   NSMutableURLRequest *request = [_httpClient multipartFormRequestWithObject:realty method:RKRequestMethodPOST path:@"/realties.json" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                        name:@"_realty[images][0]"
                                    fileName:@"test.png"
                                    mimeType:@"image/png"];
        }];

        RKObjectRequestOperation *operation = [_httpClient objectRequestOperationWithRequest:request success:nil failure:nil];
        [_httpClient enqueueObjectRequestOperation:operation];
    }
}

请求被发送到服务器,但是当我检查我的日志时,我看到“不动产”字段只有图像,没有其他属性(价格,区域等):

{"_realty"=>{"images"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x00000005817f40 @original_filename="test.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"_realty[images][0]\"; filename=\"test.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/tmp/RackMultipart20140315-28741-1yvodfr>>}}}

房地产映射:

RKObjectMapping *realtyMapping = [RKObjectMapping mappingForClass:[Realty class]];
[realtyMapping addAttributeMappingsFromArray: @[@"_id",@"user_id",@"description",@"notes",@"address",@"coords",@"pictures",@"area",@"rtype",@"rooms",@"price",@"price_m",@"title"]];

// - multiple
RKResponseDescriptor *realtiesDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:realtyMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"realties" statusCodes:statusCodes];
[_httpClient addResponseDescriptor:realtiesDescriptor];
// - single
RKResponseDescriptor *realtySingleDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:realtyMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"realty" statusCodes:statusCodes];
[_httpClient addResponseDescriptor:realtySingleDescriptor];

1 个答案:

答案 0 :(得分:2)

您必须为 Realty 添加RKRequestDescriptor对象。

RKObjectMapping* realtyMapping = <...>;
RKRequestDescriptor* reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:realtyMapping
objectClass:[Realty class]
rootKeyPath:nil
method:RKRequestMethodAny];
[[RKObjectManager sharedManager] addRequestDescriptor:reqDescriptor];
祝你好运:)