I have written a simple test case that follows Apple's documentation and I am not seeing the results that I'm expecting.
Here's the code:
- (void)testExample2
{
NSLog(@"1");
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"3");
dispatch_semaphore_signal(semaphore);
});
NSLog(@"2");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"4");
dispatch_release(semaphore);
}
I would expect to read: 1, 2, 3, 4 but instead my console just shows me 1, 3.
I've been able to work around the issue using DISPATCH_TIME_NOW
in a while loop together with a NSLoop hack but the code above should kind of work... right?
Cheers...
UPDATE
I just realised that I should be using a separate queue rather than dispatch_main_queue()