1. 程式人生 > >SDWebImage中兩個比較好用的巨集定義定義 之dispatch_main_async_safe

SDWebImage中兩個比較好用的巨集定義定義 之dispatch_main_async_safe

//主執行緒同步佇列
#define dispatch_main_sync_safe(block)\
    if ([NSThread isMainThread]) {\
        block();\
    } else {\
        dispatch_sync(dispatch_get_main_queue(), block);\
    }
//主執行緒非同步佇列
#define dispatch_main_async_safe(block)\
    if ([NSThread isMainThread]) {\
        block();\
    } else {\
        dispatch_async
(dispatch_get_main_queue(), block);\ } //用法 dispatch_main_async_safe(^{ //需要執行的程式碼片段; });