GCD, and compared with the NSOperationQueue

1 queue

1.1 Custom queue

Serial queue: queue task will only execute the order

dispatch_queue_t q = dispatch_queue_create("...", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t q = dispatch_queue_create("...", NULL);
复制代码

Parallel Queue: the queue of tasks typically performed concurrently

dispatch_queue_t q = dispatch_queue_create("......",DISPATCH_QUEUE_CONCURRENT);
复制代码

1.2 System standard queue

Global Queue: is the system of direct GET.并行

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
复制代码

The main queue: each corresponding to a unique primary application queue, direct GET. 串行 主队列It will only run on the main thread, use the main queue update UI. 主线程You may perform other tasks queue.

dispatch_queue_t q = dispatch_get_main_queue();
复制代码

2 synchronous, asynchronous, for the forward thread

dispatch_sync(_queue, ^() {
    //...
}
复制代码

3 dispatch_group

Case: There are a, b, c, d 4 asynchronous request, how to determine a, b, c, d are performed to complete? If you need a, b, c, d order execution, how to achieve?

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, ^{ /*任务a */ });
dispatch_group_async(group, queue, ^{ /*任务b */ });
dispatch_group_async(group, queue, ^{ /*任务c */ });
dispatch_group_async(group, queue, ^{ /*任务d */ });

//先提交,再用notify或者wait
dispatch_group_notify(group,dispatch_get_main_queue(), ^{
// 在a、b、c、d异步执行完成后,会回调这里
});

//dispatch_group_wait(_loggingGroup, DISPATCH_TIME_FOREVER);
复制代码

Sequentially performed, queue serial queue.

4 dispatch_barrier_async

  1. In the same Queue (queue even parallel) inside execution sequence: before the task after the task barrier, barrier task, barrier.
  2. Custom only valid queue, the queue on the system standard, the same effect as dispatch_sync.
//不使用barrier时
dispatch_queue_t queue = dispatch_queue_create("queue_name", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务1-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务2-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务3-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务4-----%@", [NSThread currentThread]);
    });
复制代码

dispatch_queue_t queue = dispatch_queue_create("queue_name", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务1-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务2-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务3-----%@", [NSThread currentThread]);
    });

    dispatch_barrier_async(queue, ^{
        NSLog(@"----barrier-----%@", [NSThread currentThread]);
    });

    dispatch_async(queue, ^{
        NSLog(@"----耗时任务4-----%@", [NSThread currentThread]);
    });

 dispatch_async(queue, ^{
        NSLog(@"----耗时任务5-----%@", [NSThread currentThread]);
    });
复制代码

5 dispatch_time

More accurate than NSTimer

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));

//dispatch_time的第二个参数的单位是纳秒

#define NSEC_PER_SEC 1000000000ull //每秒的纳秒数

复制代码

6 contrast NSOperationQueue

The relationship between 6.1 NSOperationQueue and GCD

GCD is pure C language API. NSOperationQueue is based on the GCD OC package.

Two different 6.2

  1. GCD only supports FIFO queues, NSOperationQueue can re-set the priority, maximum number of concurrent.
  2. NSOperationQueue but easy to set dependencies between operation, GCD will need a lot of code.
  3. NSOperationQueue support KVO, which can observe the execution state of the task being carried out (isExecuted), is completed (isFinished), whether to cancel (isCanceled).

6.3 Conclusion

  1. GCD is closer to the bottom, while NSOperationQueue even more high-level abstraction.
  2. GCD faster.
  3. Dependencies between tasks. GCD need to write your own code to achieve more, and have built these NSOperationQueue support.

Reproduced in: https: //juejin.im/post/5cf76dc1e51d45106b15fed4

Guess you like

Origin blog.csdn.net/weixin_34391854/article/details/91441314
gcd