GCD's Main Queue vs. Main Th
2019-02-15 本文已影响0人
大风天上来
发现
dispatch_queue_get_label()
最近在看SDWebimage代码,发现之前对线程和队列的认识不够清晰。在源码中看到这个方法。
if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(queue)) {
block();
} else {
dispatch_async(queue, block);
}
- dispatch_queue_get_label(dispatch_queue_t queue)
/** Explain: Returns the label specified for the queue when the queue was created. The label of the queue, or NULL if the queue was not provided a label during initialization. */
- DISPATCH_CURRENT_QUEUE_LABEL
/** Explain: Pass this constant to the [dispatch_queue_get_label] function to retrieve the label of the current queue. */
以前的做法
+ (void)runInMainThreadBlock:(void (^)(void))block {
if ([NSThread isMainThread]) {
block();
block = nil;
} else {
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
block = nil;
}
}