简体   繁体   中英

Making a block wait in a serial queue

I am using a serial queue in GCD like

dispatch_queue_t myCustomQueue;
myCustomQueue = dispatch_queue_create("com.example.MyCustomQueue", NULL);

dispatch_async(myCustomQueue, ^{
    ....
/* some asyncronus call*/

});

in the block i want to make a asynchronous call like playing a audio,

My main problem is as soon as i am making a asynchronous call the block is not waiting and it completes the execution of whole block.

Can i make the block wait for some time until some callback from the asynchronous call returns

The block cannot wait, but the thread who is exceuting it can.

Look into NSCondition which enables you to wait and signal threads

Basically you allocate a NSCondition and in the block you'll have [condition wait]; which will cause the thread to wait. then, when the async operation is done, you call [condition signal]; which will signal the waiting thread to continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM