GCD-継続学習中
4315 ワード
インタフェースでデータを取得した後、データ量が小さい場合はメインスレッドで処理したほうがいいですが、データ量が大きくなるとスレッドで処理する必要があります.そうしないと、使用時に明らかなカートン現象が感じられます.
1.NSThread 2.NSOperation 3.GCD
1.dispatch_の作成queue_t理解順序実行と同時実行
コード:パラメータはDISPATCH_QUEUE_CONCURRENT
実行結果:
コード:パラメータはDISPATCH_QUEUE_SERIAL
実行結果:
2.dispatch_group_tの使用
コード:
実行結果:
3.dispatch_barrier_asyncの使用
コード:
実行結果:
4.dispatch_applyの使用
コード:
実行結果:
5.dispatch_onceの使用
コード:
6.dispatch_afterの使用
コード:
実行結果:
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]