dispatch_async自定义队列永远不会退出块

tac*_*cos 5 multithreading objective-c grand-central-dispatch ios

dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_retain(callerQueue);
dispatch_queue_t downloadQueue = dispatch_queue_create("Download Queue",NULL);

dispatch_async(downloadQueue, 
^{
    //some code that accesses a web service
    dispatch_async(callerQueue,
    ^{
      //some code that accesses UI
    });
});
dispatch_release(downloadQueue);
NSLog(@"great successing!");
Run Code Online (Sandbox Code Playgroud)

问题是"非常成功!" 永远不会出现,并且在代码最外层dispatch_async块的末尾之外什么都不会发生.我不确定我做错了什么,但我知道这有些严重错误.

Jer*_*myP 4

您过早释放下载队列。您需要等到它执行完该块之后。手册dispatch_async建议将版本放在块的末尾。

请务必记住在第一次调用之前保留目标队列dispatch_async(),并在完成回调结束时释放该队列,以确保在完成回调挂起时目标队列不会被释放。