RestKit - 一次处理一个REST操作 [英] RestKit - Process one REST operation at a time

查看:106
本文介绍了RestKit - 一次处理一个REST操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RestKit 0.20.3并且有一些REST操作需要以一定的顺序完成(来自一个REST操作的响应需要包含在下一个的请求参数映射中>)。



我试着设置队列来一次处理一个操作,如下:

  RKObjectManager.sharedManager.operationQueue.maxConcurrentOperationCount = 1; 

并添加如下操作:

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

但我得到一个错误,因为第一个操作未完全其他开始。



检查日志时,它似乎像这样执行:


  1. REST操作1 - 请求映射

  2. REST操作2 - 请求映射

  3. REST操作3 - 请求映射

  4. REST操作1 - HTTP调用和响应映射

  5. REST操作2 - HTTP调用和响应映射

  6. REST操作3 - HTTP调用和响应映射

我已经尝试设置操作依赖,但是没有区别。



我需要一次完成一个REST操作。如何在RestKit中执行此操作?

解决方案

PROBLEM b
$ b

RestKit对一个REST操作使用多个 NSOperation ,所以所有请求映射将首先与问题中的代码排队。因此,当执行第一个请求映射并排队实际的HTTP请求时,它会在前两个请求映射操作之后排队。



解决方案 / p>

在第一个操作完成后,对下一个操作进行排队。



递归示例:

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

for(id objectToPost in objectsToPostInOrder){
[RKObjectManager.sharedManager postObject:objectToPost path:nil parameters:nil success:^(RKObjectRequestOperation * operation,RKMappingResult * mappingResult){
//如果一切正常,继续下一步
[self sync];
}失败:^(RKObjectRequestOperation *操作,NSError *错误){
//不继续发布,因为他们彼此依赖
[MyHUD showErrorWithStatus:error.localizedDescription];
}];

return;
}
}
}


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?

解决方案

PROBLEM

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.

SOLUTION

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

            return;
        }
    }
}

这篇关于RestKit - 一次处理一个REST操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆