iOS同時実行
1621 ワード
通常の運用では、いくつかのネットワークリクエストが同時に行われることがあります.
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); //
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, ^{
NSLog(@"-->> A");
for (int i = 0; i<100; i++) {
NSLog(@" A %d",i);
}
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(group, queue, ^{
NSLog(@"-->> B");
for (int i = 0; i<100; i++) {
NSLog(@" B %d",i);
}
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(group, queue, ^{
NSLog(@"-->> C");
for (int i = 0; i<100; i++) {
NSLog(@" C %d",i);
}
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(group, queue, ^{
NSLog(@"-->> D");
for (int i = 0; i<100; i++) {
NSLog(@" D %d",i);
}
dispatch_semaphore_signal(semaphore);
});
dispatch_group_notify(group, queue, ^{ //
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"----->>>> UI");
});