使用RestKit执行同步请求 [英] Using RestKit to perform a synchronous request

查看:87
本文介绍了使用RestKit执行同步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (BOOL)do_a_Restkit_request_and_return_a_boolean
{
     [manager postObject:nil path:@"/mypath" parameters:@{@"password":password} success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
          myResult = [mappingResult firstObject] == 5;
      } failure:^(RKObjectRequestOperation *operation, NSError *error) {
      }];

      return myResult;

}

你好,我想像上面那样拨打RestKit电话同步,以便在调用成功块之后返回myResult。

Hello I would like to make a RestKit call like the above Synchronous so as to return myResult after the call of the Success block.

推荐答案

您可以使用如下方法:

NSMutableURLRequest *request = // create a request…
RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request success:nil failure:nil];
[operation start];
[operation waitUntilFinished];

BOOL myResult = NO;

if (!operation.error) {
      myResult = [operation.mappingResult firstObject] == 5;
}

return myResult;

注意:


  • 完成块在操作完成后称为 ,因此,如果您提供成功/失败块,则在 after 方法返回$ b之前不会调用它们$ b

    • (这就是为什么我建议您将 nil 传递到完成块中的原因。)

    • The completion blocks are called after the operation is finished, so if you provide success/failure blocks, they won't be called until after the method returns
      • (This is why I suggest you pass nil into the completion blocks.)

      这篇关于使用RestKit执行同步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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