Question

In my app have an array of dictionaries containing GPS logs (100 per dictionary). I have to post these dictionaries one at a time to a server. The problem I am having is not posting which works just wonderful with RestKit, it is that I have to post them in the same order as they are in the array. This is due to an index count in the dictionaries which shouldn't be messed up on the server and in the same order as in the array.

How can I post these objects using RestKit in a synchronous manner?

Here ist the code I am using currently (asynchronous):

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);
     }];
}

Update:

I tried using this method which does work just the ways I want it to but it blocks my whole UI thread:

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)
    {
         ...
    }

}

Thanks a lot!

Was it helpful?

Solution

You should use NSOperationQueue to manage your server calls and setMaxConcurrentOperationCount to 1, like:

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

You can also take a look at this Link for further details.

OTHER TIPS

You can set the operation queue of the object managers http client to have a max concurrent count of 1. Of you can use RestKit to do the mapping and then use something else to upload.

Arguably you should modify the server so that you send the objects and associated ordering information and then it doesn't matter what order they are received in (and this will remove numerous likely headaches in the future).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top