多用GCD,少用performSelect系列方法
例如,要延后執行某項任務,可以有下面兩種實現方式,而我們應該優先考慮第二種:
// Using performSelect: withObject: afterDelay:
[self performSelector: @selector(dosomething)
withObject:nil
waitUntilDone:NO];
// Using dispatch_after
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(5.0 * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_main_queue(),^(void){
[self dosomething];
});
想把任務放在主線程中執行,也可以有下面兩種方式,而我們還是應該優先選后者:
// Using performSelectOnMainThread: withObject: waitUntilDone:
[self performSelectOnMainThread:@selector:(dosomething) withObject:nil waitUntilDone:NO];
//Using dispatch_async
//(or if waitUntilDone is YES, then dispatch_sync)
dispatch_async (dispatch_get_main_queue(),^{
[self doSomething];
});
總結
以上是生活随笔為你收集整理的多用GCD,少用performSelect系列方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优盘启动盘安装系统-(优盘启动盘安装系统
- 下一篇: AngularJS--学习笔记(一)