RestKit - 带附件的POST对象数组

时间:2014-05-19 11:23:05

标签: ios core-data restkit-0.20

我正在创建一个具有离线模式的任务应用程序。

在离线模式下,我为每个对象设置isSync属性为false,这样当网络可用时,我可以使用谓词获取这些未同步的对象,并将它们发送到服务器。

问题是我无法为每个对象添加附件。

在线我正在发送POST请求,附件包含以下单个对象:

if (attachments != nil && attachments.count > 0) {
    task.total_attachments = [NSNumber numberWithInteger:attachments.count];
    NSMutableURLRequest *request =[[RKObjectManager sharedManager] multipartFormRequestWithObject:task
                                                                                           method:RKRequestMethodPOST
                                                                                             path:URL_TASKS
                                                                                       parameters:nil
                                                                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                                                            int counter = 0;
                                                                            for (NSDictionary *dic in attachments) {
                                                                                [formData appendPartWithFileData:UIImageJPEGRepresentation([dic objectForKey:@"image"], 0.7)
                                                                                                            name:[NSString stringWithFormat:@"attachment[%i]", counter]
                                                                                                        fileName:[dic objectForKey:@"name"]
                                                                                                        mimeType:@"image/jpg"];
                                                                                counter++;
                                                                            }
                                                                        }];

    RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] managedObjectRequestOperationWithRequest:request
                                                                                               managedObjectContext:[NSManagedObjectContext MR_defaultContext]
                                                                                                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                                                                                KLog(@"success");

                                                                                                                completionHandler((DBTasks *)[mappingResult firstObject], nil);

                                                                                                            } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                                                                                KLog(@"fail");
                                                                                                                completionHandler(nil, error);
                                                                                                            }];
    [operation start];

}

似乎每个请求都添加了附件,而不是每个对象。现在,当我发布一个对象数组而不是单个对象时,如何为每个对象添加附件?这样服务器就可以决定为哪个对象添加哪个附件。

0 个答案:

没有答案