I want to wrap an async API that look like this:
[someObject completeTaskWithCompletionHandler:^(NSString *result) {
}];
into a synchronous m
dispatch_semaphore_wait
blocks the main queue in your example. You can dispatch the async task to a different queue:
__block NSString *returnResult;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0UL);
dispatch_async(queue,^{
result = [someObject completeTaskSynchronously];
});
Or use some other system, like NSRunLoop:
__block finished = NO;
[self completeTaskWithCompletionHandler:^(NSString *result) {
resultResult = result;
finished = YES;
}];
while (!finished) {
// wait 1 second for the task to finish (you are wasting time waiting here)
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}