【问题标题】:RestKit: Post multiple objects synchronouslyRestKit:同步发布多个对象
【发布时间】:2014-05-20 03:54:46
【问题描述】:

在我的应用中有一组包含 GPS 日志的字典(每个字典 100 个)。我必须一次将这些字典发布到服务器。我遇到的问题不是发布与 RestKit 配合得很好,而是我必须以与它们在数组中相同的顺序发布它们。这是由于字典中的索引计数不应在服务器上被弄乱,并且与数组中的顺序相同。

如何使用 RestKit 以同步方式发布这些对象?

这是我目前正在使用的代码(异步):

for (int i = 0; i < [arrayOfGPSLogChunks count]; i++)
{
    NSMutableDictionary *historyToUpload = [[NSMutableDictionary alloc] init];
    [historyToUpload setObject:driveID forKey:@"driveID"];
    [historyToUpload setObject:[arrayOfGPSLogChunks objectAtIndex:i] forKey:@"gpsLog"];

    [[RKObjectManager sharedManager] postObject:nil
                                           path:@"api/drives/addDriveChunk"
                                     parameters:historyToUpload
                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
     {
         RKLogInfo(@"Successfully Uploaded Drive Chunk %i/%i", i,arrayOfGPSLogChunks.count);
     }
                                        failure:^(RKObjectRequestOperation *operation, NSError *error)
     {
         RKLogError(@"Failed Uploading Drive Chunk (%i): (Error: %@)", i, error);
     }];
}

更新:

我尝试使用这种方法,它确实按我想要的方式工作,但它阻塞了我的整个 UI 线程:

for (int i = 0; i < [arrayOfGPSLogChunks count]; i++)
{
    NSMutableDictionary *historyToUpload = [[NSMutableDictionary alloc] init];
    [historyToUpload setObject:driveID forKey:@"driveID"];
    [historyToUpload setObject:[arrayOfGPSLogChunks objectAtIndex:i] forKey:@"gpsLog"];

    NSData *requestData = [NSJSONSerialization dataWithJSONObject:historyToUpload options:NSJSONWritingPrettyPrinted error:nil];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kUploadDrivesChunkURL]
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kConnectionTimeOut];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil];
    [operation start];
    [operation waitUntilFinished];

    if (operation.error)
    {
         ...
    }

}

非常感谢!

【问题讨论】:

    标签: ios post upload restkit synchronous


    【解决方案1】:

    你应该使用NSOperationQueue来管理你的服务器调用和setMaxConcurrentOperationCount到1,比如:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:1];
    

    您也可以查看Link 了解更多详情。

    【讨论】:

      【解决方案2】:

      您可以将对象管理器http客户端的操作队列设置为最大并发数为1。您可以使用RestKit进行映射,然后使用其他东西上传。

      可以说,您应该修改服务器,以便发送对象和相关的订购信息,然后无论以什么顺序接收它们都无关紧要(这将在未来消除许多可能令人头疼的问题)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多