通过接口获取数据后,如果数据量小在主线程中处理还好,如果数据量变大后就需要在分线程中处理,不然使用时,会感觉到明显的卡顿现象。
IOS :多线程处理方法
1.NSThread
2.NSOperation
3.GCD
今天学习一下GCD
1.创建dispatch_queue_t 理解顺序执行和并发执行
//serial queue 顺序执行先 进先出 FIFO DISPATCH_QUEUE_SERIAL
//concurrent queue 并发执行 看各自任务的耗时时间 DISPATCH_QUEUE_CONCURRENT
代码:参数为DISPATCH_QUEUE_CONCURRENT
NSDate *date = [NSDate date];
NSString *dateString = [date description];
const char *queuename = [dateString UTF8String];
dispatch_queue_t myqueue = dispatch_queue_create(queuename, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(myqueue, ^{
[self downloadTime:6];
});
dispatch_async(myqueue, ^{
[self downloadTime:3];
});
dispatch_async(myqueue, ^{
[self downloadTime:2];
});
执行结果:
2002016-06-22 17:21:13.613 Learn-IOS[24089:546819] 我下载了2秒
2016-06-22 17:21:14.610 Learn-IOS[24089:546712] 我下载了3秒
2016-06-22 17:21:17.609 Learn-IOS[24089:546718] 我下载了6秒
代码:参数为DISPATCH_QUEUE_SERIAL
NSDate *date = [NSDate date];
NSString *dateString = [date description];
const char *queuename = [dateString UTF8String];
dispatch_queue_t myqueue = dispatch_queue_create(queuename, DISPATCH_QUEUE_SERIAL);
dispatch_async(myqueue, ^{
[self downloadTime:6];
});
dispatch_async(myqueue, ^{
[self downloadTime:3];
});
dispatch_async(myqueue, ^{
[self downloadTime:2];
});
执行结果:
2002016-06-22 17:21:13.613 Learn-IOS[24089:546819] 我下载了6秒
2016-06-22 17:21:14.610 Learn-IOS[24089:546712] 我下载了3秒
2016-06-22 17:21:17.609 Learn-IOS[24089:546718] 我下载了2秒
2.dispatch_group_t 的使用
dispatch_group_t 并发执行任务,等所有任务完成后给出通知
代码:
//DISPATCH_QUEUE_PRIORITY_DEFAULT 优先考虑的
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[self downloadTime:7];
});
dispatch_group_async(group, queue, ^{
[self downloadTime:1];
});
dispatch_group_async(group, queue, ^{
[self downloadTime:4];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"全部下载完了,要更新UI了,记住在主线程里更新UI");
});
执行结果:
2002016-06-22 17:26:17.404 Learn-IOS[24231:554422] 我下载了1秒
2016-06-22 17:26:20.401 Learn-IOS[24231:554436] 我下载了4秒
2016-06-22 17:26:23.402 Learn-IOS[24231:554430] 我下载了7秒
2016-06-22 17:26:23.402 Learn-IOS[24231:554388] 全部下载完了,要更新UI了,记住要在主线程里更新UI哈
3.dispatch_barrier_async 的使用
是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行
代码:
dispatch_queue_t queue = dispatch_queue_create("lsb", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[self downloadTime:2];
});
dispatch_async(queue, ^{
[self downloadTime:3];
});
dispatch_barrier_sync(queue, ^{
[self downloadTime:2];
});
dispatch_async(queue, ^{
[self downloadTime:1];
});
执行结果:
2016-06-22 17:32:13.131 Learn-IOS[24484:564450] 我下载了2秒
2016-06-22 17:32:14.130 Learn-IOS[24484:564457] 我下载了3秒
2016-06-22 17:32:20.132 Learn-IOS[24484:564404] 我下载了6秒
2016-06-22 17:32:20.132 Learn-IOS[24484:564404] 我下载了1秒
4.dispatch_apply 的使用
重复执行某个任务
代码:
//重复执行耗时任务
dispatch_queue_t queue = dispatch_queue_create("lsb", DISPATCH_QUEUE_SERIAL);
dispatch_apply(5, queue, ^(size_t index) {
// 执行5次
[self downloadTime:index];
});
执行结果:
2016-06-22 17:33:27.505 Learn-IOS[24546:566534] 我下载了0秒
2016-06-22 17:33:28.506 Learn-IOS[24546:566534] 我下载了1秒
2016-06-22 17:33:30.507 Learn-IOS[24546:566534] 我下载了2秒
2016-06-22 17:33:33.508 Learn-IOS[24546:566534] 我下载了3秒
2016-06-22 17:33:37.509 Learn-IOS[24546:566534] 我下载了4秒
5.dispatch_once 的使用
在程序的生命周期里只执行一次,单利时可以使用
代码:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
6.dispatch_after 的使用
延时执行某个任务
代码:
double delayInSeconds = 2.0;
dispatch_time_t poptime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds*NSEC_PER_SEC);
NSLog(@"开始执行");
dispatch_after(poptime, dispatch_get_main_queue(), ^{
NSLog(@"延时操作");
});
执行结果:
2016-06-22 17:37:45.208 Learn-IOS[24618:574357] 开始执行
2002016-06-22 17:37:47.206 Learn-IOS[24618:574357] 延时操作