3

需要对以下代码进行单元测试,dispatch_async 表示代码不会按应用逻辑顺序执行,请问如何使其及时运行?

谢谢你。

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [AdTracker dosomething];
    });
4

1 回答 1

2

http://www.mikeash.com/pyblog/friday-qa-2011-07-22-writing-unit-tests.html

+ (BOOL)waitFor2:(finishBlock)block {
NSTimeInterval timeoutInSeconds = 10.0;
NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds];

while (!block() && ([giveUpDate timeIntervalSinceNow] > 0)) {
    NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
    [[NSRunLoop currentRunLoop] runUntilDate:stopDate];   // un-blocking.
    DLog(@"+++++  %@", [NSDate date]);
}   

return block();

}

于 2012-07-17T12:43:01.040 回答