Dispatch_sync(dispatch_get_main_queue,^{})死锁的问题
dispatch_sync的意思像上边解释的一样,就是向特定的queue中插入一个block,等到这个block被执行了,再继续下面的任务,但是如果这个被安排任务的queue是currentQueue,那么将会成为死锁,因为sync会block currentqueue所以现在线程被block了,所以不会再继续执行下去。下面是搜到的英文解释。
dispatch_sync does two things:
- queue a block
- blocks the current thread until the block has finished running
Given that the main thread is a serial queue (which means it uses only one thread), the following statement:
dispatch_sync(dispatch_get_main_queue(), ^(){/*...*/});
will cause the following events:
- dispatch_sync queues the block in the main queue.
- dispatch_sync blocks the thread of the main queue until the block finishes executing.
- dispatch_sync waits forever because the thread where the block is supposed to run is blocked.
The key to understanding this is that dispatch_sync does not execute blocks, it only queues them. Execution will happen on a future iteration of the run loop.
还有,之所以要在mainqueue中去刷新界面,是因为有可能很多线程在后台运行,而UI的刷新永远是在主线程中去完成,所以UI的刷新如果被放到了其他的后台线程中,那么刷新就无法执行,所以必须要放在主线程中去做。