문제

Need to do unit testing for the following code, dispatch_async means code won't be executed by app logic sequence, any idea on how to make it run timely?

Thank you.

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [AdTracker dosomething];
    });
도움이 되었습니까?

해결책

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

}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top