Why does the block after dispatch_async(dispatch_get_main_queue() never get called?
<p>I want to execute some code asynchronously and have for this reason started using GCD for OSX/iOS.<br>
Currently I am using the function <em>dispatch_async()</em>.<br>
When I want to execute something concurrently on another thread, I use the function <em>dispatch_get_global_queue()</em>.<br>
When I want to dispatch the result to the main thread, I use the function <em>dispatch_get_main_queue()</em>.<br>
But the results never arrive to the main thread.</p>

<p>When setting up breakpoints in the debugger (at the <em>dispatch_async</em> lines), the block after function <em>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)</em> 
gets executed only sometimes, but often the debugger just ignores it.<br>
When the block does get executed and execution flow gets to the breakpoint at <em>dispatch_async(dispatch_get_main_queue()</em>, the block after that always gets ignored.</p>

<p>Without <em>dispatch_async(dispatch_get_global_queue)</em> and <em>dispatch_async(dispatch_get_main_queue)</em> the code executes as it should, albeit synchronously.</p>

<p>My question is why does <em>dispatch_async(dispatch_get_main_queue()</em> inside of the <em>dispatch_async(dispatch_get_global_queue()</em> block never get executed?<br>
And on the same note, why is the <em>dispatch_async(dispatch_get_global_queue()</em> block not getting executed every time?</p>

<p>My development environment is<br>
OS: <em>OS X 10.11.3</em><br>
IDE:              <em>Xcode 7.2</em><br>
Compiler:         <em>Apple LLVM version 7.0.2 (clang-700.1.81)</em><br>
Target:           <em>x86_64-apple-darwin15.3.0</em><br></p>

<p>Here is a simple example which reproduces the erratic behavior (an OS X console application):</p>

<p><em>TestClass.h</em><br></p>

<pre><code>#ifndef TestClass_h
#define TestClass_h

@interface TestClass : NSObject {
}

- (void)testMethod:(NSString *)testString withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock;

@end

#endif
</code></pre>

<p><em>TestClass.m</em><br></p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;
#import "TestClass.h"

@implementation TestClass

- (void)testMethod:(NSString *)testString withCompletionBlock:(void(^)(NSString *blockResult, NSError __autoreleasing *error))completionBlock {
    __block NSString *stringResult = nil;

    if (completionBlock) {
        __block NSError *resultError = nil;


       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // THIS BLOCK IS CALLED ONLY SOMETIMES, MOST OF THE TIME IT IS IGNORED.

            if ([testString isEqual: @"Error string"]) {
                NSDictionary *errorUserInfo = @{ NSLocalizedDescriptionKey: @"This is an error.", NSLocalizedFailureReasonErrorKey: @"", NSLocalizedRecoverySuggestionErrorKey: @"" };                
                resultError = [[NSError alloc] initWithDomain:@"com.test.TestErrorDomain" code:10 userInfo:errorUserInfo];
            }
            else {
                stringResult = testString;
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                // THIS BLOCK NEVER GETS EXECUTED.

                completionBlock(stringResult, resultError);
            });
        });
    }
}

@end
</code></pre>

<p><em>main.m</em></p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;
#import "TestClass.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block NSString *resultString = nil;
        TestClass * testObject = [[TestClass alloc] init];

        // Output for this call should be: The result string is: Test string.
        [testObject testMethod:@"Test string" withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error) {
            resultString = blockString;

            if (resultString) {
                NSLog(@"The result string is: %@.", resultString);
            }
        }];

        // Output for this call should be: Error: This is an error.
        [testObject testMethod:@"Error string" withCompletionBlock:^(NSString *blockString, NSError __autoreleasing *error) {
            resultString = blockString;

            if (!resultString) {
                if (error) {
                    NSLog(@"Error: %@", [error localizedDescription]);
                }
                else {
                    NSLog(@"Error not recognized!");
                }
            }
        }];
    }

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