thejaz thejaz - 3 months ago 38
iOS Question

RestKit - Process one REST operation at a time

I'm using RestKit 0.20.3 and have some REST operations that needs to be done in a certain order (the response from one REST operation needs to be included in the request parameter mapping for the next).

I tried setting up the queue to handle one operation at a time like this:

RKObjectManager.sharedManager.operationQueue.maxConcurrentOperationCount = 1;

And adding the operations like this:

for (id insertedObject in insertedObjects) {
[RKObjectManager.sharedManager postObject:insertedObject path:nil parameters:nil success:nil failure:nil];

But I get an error, because the first operation is not fully completed before the other start.

When inspecting the logs, it seems like it is executed like this:

  1. REST operation 1 - Request mapping

  2. REST operation 2 - Request mapping

  3. REST operation 3 - Request mapping

  4. REST operation 1 - HTTP call and response mapping

  5. REST operation 2 - HTTP call and response mapping

  6. REST operation 3 - HTTP call and response mapping

I have already tried setting operation dependencies, but that does not make a difference.

I need one REST operation to be completed at a time. How do I do this in RestKit?



RestKit uses multiple NSOperation for one REST operation, so all request mappings will be queued first with the code in the question. So when the first request mapping is executed and queuing the actual HTTP request, it gets queued behind the first two request mapping operations.


Queue the next operation after the first one finishes.

Example with recursion:

- (void)sync {
    NSArray *objectsToPostInOrder = ...;

        for (id objectToPost in objectsToPostInOrder) {
            [RKObjectManager.sharedManager postObject:objectToPost path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                // Proceed with next if everything went OK
                [self sync];
            } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                // Don't continue posting as they are dependent on each other
                [MyHUD showErrorWithStatus:error.localizedDescription];