iOS主线程操作的几种方法

在实际开发中,经常需要异步处理数据,然后刷新UI只能在主线程,以下是几种主线程操作的方法,gcd方法是大家最熟悉的。


1. GCD方法(最常见,使用简单方便,苹果封装)

dispatch_async(dispatch_get_main_queue(), ^{

     //do something

});

2.NSOperation方法

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];主队列

NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

//do something

}];

[mainQueue addOperation:operation];//不要忘记加这句

3. NSThread方法

[selfperformSelector:@selector(method) onThread:[NSThread mainThread] withObject:nilwaitUntilDone:YESmodes:nil];

[selfperformSelectorOnMainThread:@selector(method) withObject:nilwaitUntilDone:YES];

[[NSThread mainThread] performSelector:@selector(method) withObject:nil];

4. RunLoop方法

[[NSRunLoop mainRunLoop] performSelector:@selector(method) withObject:nil];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容