iOS开发之GCD同步主线程、异步主线程

时间:2021-06-25 21:47:39

/** 在主线程执行block */

+ (void)gs_synExecuteOnMainThread:(void (^)(void))block {

if ((nil == block) || (NULL == block)) {

return;

}

if ([NSThread isMainThread]) {

block();

}else {

dispatch_sync(dispatch_get_main_queue(), ^{

block();

});

}

}

/** 在异步主线程执行block */

+ (void)gs_asynExecuteOnMainThread:(void (^)(void))block {

if ((nil == block) || (NULL == block)) {

return;

}

dispatch_async(dispatch_get_main_queue(), ^{

block();

});

}