iOS / Objective-C:从异步方法获取回叫

时间:2018-09-06 19:26:34

标签: ios objective-c completionhandler

在我的应用程序中的某个时刻,我调用了超类中的方法以对服务器进行身份验证。在此方法的最后,我想运行一些特定于调用它的类的代码。这是在调用附加代码之前等待超类的响应的最佳方法吗?

     [super authenticateWithServer^(BOOL success, NSError *error) {
             dispatch_async(dispatch_get_main_queue(), ^{
             NSLog(@"heard back from method");
if (success==YES) {
//RUN MY CODE HERE
}
             });
             }];

如果是这样,该方法将是什么样?像下面这样?

-(BOOL)authenticateWithServer (
//if fail {
return NO;
}
else {
return YES;
}
}

1 个答案:

答案 0 :(得分:1)

BOOL返回类型不与异步操作混合使用。相反,您需要将与服务器进行身份验证的结果传递到完成模块中,并让调用方对其进行检查。我强烈建议您查看http://goshdarnblocksyntax.com,了解正确的块语法。

我很确定这与您要执行的操作很接近(在此处添加了模拟服务器请求的假延迟):

@interface ViewController ()
@property (strong, nullable) IBOutlet UILabel *resultLabel;
- (void)authenticateWithServer:(void (^_Nullable)(BOOL success, NSError *_Nullable error))completion;
- (void)_fakeUrlRequestToServerWithCompletion:(void(^_Nullable)(BOOL successFromServer, NSError *_Nullable errorFromServer))serverCompletion;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)authWithServerButton:(id)sender {
    // call to authenticate with the results in the callback (not returning BOOL)
    [self authenticateWithServer:^(BOOL success, NSError * _Nullable error) {
        // callback will probably come in off the main queue so if you're doing some UI updates jump back on the main queue
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.resultLabel.text = [NSString stringWithFormat:@"Result = %@\nError = %@",success == 1 ? @"SUCCESS" : @"FAILURE", error == nil ? @"No Error" : error];
        }];
    }];
}

- (void)authenticateWithServer:(void (^_Nullable)(BOOL success, NSError *error))completion {
    // put this on the background queue so it doesn't hug
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        // call the fakeUrlRequestToServer method to simulate your async request
        [self _fakeUrlRequestToServerWithCompletion:^(BOOL successFromServer, NSError * _Nullable errorFromServer) {
            // completion block that was passed into the method, callback and pass along the success and error results
            completion(successFromServer, errorFromServer);
        }];
    });
}

- (void)_fakeUrlRequestToServerWithCompletion:(void(^_Nullable)(BOOL successFromServer, NSError *errorFromServer))serverCompletion {
    // fake sleep here for 2 seconds just to simulate waiting for the callback
    // never call sleep in your own code
    sleep(2);
    NSError *fakeError = nil;
    // just a fake auth success or failure
    BOOL fakeSuccess = arc4random() % 2 == 1 ? YES : NO;
    if (fakeSuccess == NO) {
        // fake error
        fakeError = [NSError errorWithDomain:@"FakeErrorDomain" code:22 userInfo:nil];
    }
    // completion block that was passed into the method, call back with the success and error params passed in
    serverCompletion(fakeSuccess, fakeError);
}


@end

下面是一个实际的例子:

Fake Async Callback

编辑:由于在我给的示例中补全代码段可以为空,因此您需要检查是否先传入了一个。

  if (completion) {
    completion(success, error);
  }