首页 > 移动平台 > 详细

ios 将一个函数在主线程执行的4种方法

时间:2018-03-10 20:49:56      阅读:246      评论:0      收藏:0      [点我收藏+]
  • GCD方法,通过向主线程队列发送一个block块,使block里的方法可以在主线程中执行。
dispatch_async(dispatch_get_main_queue(), ^{  //需要执行的方法 });
  • NSOperation 方法
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];  //主队列NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ //需要执行的方法 }]; [mainQueue addOperation:operation];
  • NSThread 方法
[self performSelector:@selector(method) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES modes:nil]; [self performSelectorOnMainThread:@selector(method) withObject:nil waitUntilDone:YES]; [[NSThread mainThread] performSelector:@selector(method) withObject:nil];
  • RunLoop方法
[[NSRunLoop mainRunLoop] performSelector:@selector(method) withObject:nil];

ios 将一个函数在主线程执行的4种方法

原文:https://www.cnblogs.com/linusflow/p/8541707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!