How to wrap an asynchronous method that takes a block and turn it synchronous in Objective-C

后端 未结 4 957
感情败类
感情败类 2021-02-02 18:03

I want to wrap an async API that look like this:

[someObject completeTaskWithCompletionHandler:^(NSString *result) {

}];

into a synchronous m

4条回答
  •  你的背包
    2021-02-02 18:18

    Using an NSRunLoop is the easiest to do here.

    __block NSString* result = nil;
    [self completeTaskWithCompletionHandler:^(NSString *resultstring) {
        result = resultstring;
    }];
    while (!result) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    

提交回复
热议问题