iOS进阶之路iOS

iOS主线程操作的几种方法

2016-11-29  本文已影响72人  蜡笔小强

在实际开发中,经常需要异步处理数据,然后刷新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];

上一篇 下一篇

猜你喜欢

热点阅读