//主线程同步队列
#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(^{
                    //需要执行的代码片段;
                });

或者

#ifndef dispatch_main_async_safe
#define dispatch_main_async_safe(block)\
    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);\
    }
#endif

 

相关文章:

  • 2021-11-03
  • 2021-04-14
  • 2021-06-25
  • 2021-06-18
  • 2021-08-30
  • 2022-01-15
  • 2021-08-09
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案