- 主线程同步,避免死锁
1
2
3
4
5
6
7
8
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
block();\
} else {\
dispatch_sync(dispatch_get_main_queue(), block);\
}
主线程异步
1
2
3
4
5
6
7
8
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}dispatch_queue_get_label
1
2
3
4
5
6
7
8
9
10
11
12
13* @abstract
* Returns the label of the given queue, as specified when the queue was
* created, or the empty string if a NULL label was specified.
*
* Passing DISPATCH_CURRENT_QUEUE_LABEL will return the label of the current
*
*
* @param queue
* The queue to query, or DISPATCH_CURRENT_QUEUE_LABEL.
// 返回指定队列名称,为空返回NULL
// 入参为队列 or DISPATCH_CURRENT_QUEUE_LABEL
// DISPATCH_CURRENT_QUEUE_LABEL:表示返回当前队列名称