iOS Stop block execution from progressing until block has finished
<p>I have a method in an iOS app that is supposed to return a bool value depending upon whether or not a web call succeeds.</p>

<p>The web call is structured in a way such that it takes a block as a callback parameter and that callback is called when the web call has a result. Based on that result my method needs to return a True/False value.</p>

<p>So, I need to stop execution from progressing any further without first having a result to return.</p>

<p>I am trying to achieve this via semaphores, after looking at some examples that others have shared, but the callback is never called, if I remove the semaphore then the callback is always called.</p>

<p>What am I missing here?</p>

<pre><code>+ (BOOL)getUserInformation {
__block BOOL flag = false;

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[[WebServicesManager sharedManager] getUserInformationWithCallback:^(NSInteger statusCode, NSString *response, NSDictionary *responseHeaders, id obj, NSError *error) {

    if (error) {
        //Handle error case and perform appropriate cleanup actions.
    }
    else
    {
        //Save user information
        flag = true;
    }

    dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

return flag;
}
</code></pre>

<p>I put a break point on <code>if(error)</code> to check if the callback gets called, it doesnt, unless I remove the semaphore.</p>

<p>I could give this method its own callback block or I could give the containing class a delegate and achieve what I need but I would really like to make this approach work.</p>