Grand Central调度和unit testing

我已经写了一个简单的testing用例,遵循苹果的文档 ,我没有看到我期待的结果。

代码如下:

- (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); } 

我希望阅读:1,2,3,4,而是我的控制台只显示我1,3。

我已经能够在一个while循环中使用DISPATCH_TIME_NOW和NSLoop hack一起解决这个问题,但是上面的代码应该是一种工作…对吧?

干杯…

UPDATE

我只是意识到,我应该使用一个单独的队列,而不是dispatch_main_queue()

tearDown结束了以下事情。

 while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW)) { NSLog(@"...Tearing Down Tests.."); [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]]; } dispatch_release(semaphore); 

并把信号量创build成setUp