GCD的简单应用

1.耗时操作
通常我们需要做耗时操作的时,可以直接使用下面代码,GCD会自动帮我们创建线程进行处理

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //耗时操作
        NSLog(@"1");
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI
            NSLog(@"2");
        });
    });

2.阻塞式(有先后顺序)操作
若遇到任务有先后顺序,可以考虑barrier,阻塞式
eg: 流程任务,需要多个网络请求的值进行汇总,再进行网络请求,再汇总。
下面的任务顺序就是 123顺序任意改变 接着永远是4444444 再 567顺序任意改变 最后永远是8888888

    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
    });
    dispatch_async(queue, ^{
        NSLog(@"3");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"444444");
            dispatch_async(queue, ^{
                NSLog(@"5");
            });
            dispatch_async(queue, ^{
                NSLog(@"6");
            });
            dispatch_async(queue, ^{
                NSLog(@"7");
            });
            dispatch_barrier_async(queue, ^{
                    NSLog(@"888888");
        });
    });

这样写也是可以的

    dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
    });
    dispatch_async(queue, ^{
        NSLog(@"3");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"444444");

    });
    dispatch_async(queue, ^{
        NSLog(@"5");
    });
    dispatch_async(queue, ^{
        NSLog(@"6");
    });
    dispatch_async(queue, ^{
        NSLog(@"7");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"888888");
    });

3.1调度组:一组代码完成后统一做处理
dispatch_group_notify是异步的来监听group调度组中的人物完成情况,若所有人物完成则会调用notify中的任务,但是notify后面的任务还是会先执行,但后面的任务在代码中是主线程不是子线程

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t global = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, global, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"任务一:%@",[NSThread currentThread]);
        
    });
    dispatch_group_async(group, global, ^{
        NSLog(@"任务二:%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, global, ^{
        NSLog(@"任务三:%@",[NSThread currentThread]);
    });
    
    dispatch_group_notify(group, global, ^{
        NSLog(@"一二三都完成");
    });
    
    NSLog(@"异步代码");

3.2调度组
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
是同步的等待,当调度组中的任务全部执行完毕的时候才会调用wait后面的语句,

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t global = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, global, ^{
        [NSThread sleepForTimeInterval:1.0];
        NSLog(@"任务一:%@",[NSThread currentThread]);
        
    });
    dispatch_group_async(group, global, ^{
        NSLog(@"任务二:%@",[NSThread currentThread]);
    });
    dispatch_group_async(group, global, ^{
        NSLog(@"任务三:%@",[NSThread currentThread]);
    });
    
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    
    NSLog(@"异步代码");

调度组总结:
wait可以理解为sync
notify可以理解为async

调度组还有对应的 enter leave方法 不过并不是很实用

你可能感兴趣的:(GCD的简单应用)