【发布时间】: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