完成处理程序和返回值

完成处理程序和返回值

本文介绍了完成处理程序和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话,将其完成处理程序返回一个值的方法。该方法异步执行,我不希望方法的所有身体已经执行之前返回一个值。下面是一些故障 code来说明什么,我想实现:

I want to call a method which will return a value from its completion handler. The method performs asynchronously and I don't want to return a value before all the body of the method has been executed. Here is some faulty code to illustrate what I am trying to achieve:

// This is the way I want to call the method
NSDictionary *account = [_accountModel getCurrentClient];

// This is the faulty method that I want to fix
- (NSDictionary *)getCurrentClient
{
    __block NSDictionary *currentClient = nil;
    NXOAuth2Account *currentAccount = [[[NXOAuth2AccountStore sharedStore] accounts] lastObject];

    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:[NSString stringWithFormat:@"%@/clients/%@", kCatapultHost, currentAccount.userData[@"account_name"]]]
                    usingParameters:nil
                        withAccount:currentAccount
                sendProgressHandler:nil
                    responseHandler:^ (NSURLResponse *response, NSData *responseData, NSError *error) {
                        NSError *jsonError;

                        currentClient = [NSJSONSerialization JSONObjectWithData:responseData
                                                                        options:kNilOptions
                                                                            error:&jsonError];
                    }];

    return currentClient;
}

我不想,直到 NXOAuth2Request 已完成 getCurrentClient 方法返回一个值。我不能返回当前客户端的请求的响应处理程序中无论是。那么,什么是我的选择?

I don't want the getCurrentClient method to return a value until the NXOAuth2Request has finished. I can't return the current client inside the response handler of the request either. So what are my options?

推荐答案

您需要修改 getCurrentClient 来参加一个完成的块,而不是返回值。

You need to change getCurrentClient to take in a completion block instead of returning a value.

例如:

-(void)getCurrentClientWithCompletionHandler:(void (^)(NSDictionary* currentClient))handler
{
    NXOAuth2Account *currentAccount = [[[NXOAuth2AccountStore sharedStore] accounts] lastObject];

    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:[NSString stringWithFormat:@"%@/clients/%@", kCatapultHost, currentAccount.userData[@"account_name"]]]
                   usingParameters:nil
                       withAccount:currentAccount
               sendProgressHandler:nil
                   responseHandler:^ (NSURLResponse *response, NSData *responseData, NSError *error) {
                        NSError *jsonError;

                        NSDictionary* deserializedDict = [NSJSONSerialization JSONObjectWithData:responseData
                                                                                         options:kNilOptions
                                                                                           error:&jsonError];
                        handler(deserializedDict);
                }];
}

记住 getCurrentClient 将立即返回,而网络请求是在另一个线程调度是很重要的。不要忘记,如果你想用你的响应处理程序更新UI,你需要有你的处理器主线程

It's important to remember that getCurrentClient will return immediately, while the network request is dispatched on another thread. Don't forget that if you want to update the UI using your response handler, you need to have your handler run on the main thread.

这篇关于完成处理程序和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!