Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

ios - How to force GCD perform current blocks on main queue?

I have worker thread task that published results on main queue using:

// on worker queue

// (long-running task)

// publish results on main queue
dispatch_async(dispatch_get_main_queue(), ^(void) {
    _finished = YES;
});

How can i sit in main queue (main CXTest thread) and force gcd handle current tasks on the queue like:

// on main queue
while (!_finished) {
    [NSThread sleepForTimeInterval:0.1];
    // TODO : force main queue to perform current blocks
}

The obvious solution is to set _finished in worker thread, but i can't do that for some reason (UI controls can be touched in main thread only)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can pump your run loop like this:

while (!_finished) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1];
}

it will drain main queue eventually, setting _finished to YES.

But I'd recommend you to use an async test helper like XCAsyncTextCase, also expecta has quite handy async helper. Other test frameworks may have such extensions as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...