通過接口獲取數據后,如果數據量小在主線程中處理還好,如果數據量變大后就需要在分線程中處理,不然使用時,會感覺到明顯的卡頓現象。
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] 延時操作