1. 程式人生 > >iOS開發之GCD同步主線程、異步主線程

iOS開發之GCD同步主線程、異步主線程

turn void nil patch null 異步 同步 oid spa

/** 在主線程執行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();

});

}

iOS開發之GCD同步主線程、異步主線程