GCD整理

作者: 走着走着就会敲代码了 | 来源:发表于2019-05-23 11:50 被阅读0次

这边就整理下GCD一些常用的方式,以及举例子并打印结果说明相应的。一直想去整理然后嘛,要么没时间要么懒...拖到现在,em...别的就不扯了。关于GCD这边就不一一介绍了,有需要可以参考一下这篇帖子写的很赞,就是感觉少了点意思吧。
本文主要是针对一下几种讲解说明:

  • 同步 + 串行队列
  • 同步 + 并发队列
  • 异步 + 串行队列
  • 异步 + 并发队列
  • 异步并发 + 统一回调
    主要是上面这几点,基本就是代码。

同步 + 串行队列

- (void)syncSerialQueue {
    // 不会开启新线程,在当前线程执行任务(也是在主线程执行)
    NSLog(@"sync serial---begin,%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.serial", DISPATCH_QUEUE_SERIAL);
    // 任务1
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    NSLog(@"sync serial---end,%@", [NSThread currentThread]);
}

运行结果:

2019-05-23 10:15:30.727191+0800 GCD[657:120690] sync serial---begin,<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:32.728574+0800 GCD[657:120690] 任务1 sync serial for:0 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:34.730104+0800 GCD[657:120690] 任务1 sync serial for:1 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:36.731658+0800 GCD[657:120690] 任务1 sync serial for:2 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:38.733100+0800 GCD[657:120690] 任务2 sync serial for:0 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:40.734532+0800 GCD[657:120690] 任务2 sync serial for:1 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:42.735965+0800 GCD[657:120690] 任务2 sync serial for:2 queue:<NSThread: 0x170073240>{number = 1, name = main}
2019-05-23 10:15:42.736199+0800 GCD[657:120690] sync serial---end,<NSThread: 0x170073240>{number = 1, name = main}

分析如下:

  • 所有任务都是在当前线程(主线程)中执行的,并没有开启新的线程(同步执行不具备开启新线程的能力)。
  • 同步任务需要等待队列的任务执行结束
  • 任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。

同步 + 并发队列

- (void)syncConcurrentQueue {
    // 主线程中执行,一个个执行完成
    NSLog(@"sync serial---begin,%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.concurrent", DISPATCH_QUEUE_CONCURRENT);
    // 任务1
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_sync(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });

    NSLog(@"sync serial---end,%@", [NSThread currentThread]);
}

运行结果:

2019-05-23 10:16:45.123312+0800 GCD[659:121053] sync serial---begin,<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:47.124746+0800 GCD[659:121053] 任务1 sync serial for:0 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:49.126217+0800 GCD[659:121053] 任务1 sync serial for:1 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:51.127658+0800 GCD[659:121053] 任务1 sync serial for:2 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:53.128392+0800 GCD[659:121053] 任务2 sync serial for:0 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:55.129833+0800 GCD[659:121053] 任务2 sync serial for:1 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:57.131300+0800 GCD[659:121053] 任务2 sync serial for:2 queue:<NSThread: 0x17006c300>{number = 1, name = main}
2019-05-23 10:16:57.131557+0800 GCD[659:121053] sync serial---end,<NSThread: 0x17006c300>{number = 1, name = main}

分析如下:

  • 所有任务都是在当前线程(主线程)中执行的,没有开启新的线程(同步执行不具备开启新线程的能力)。
  • 同步任务需要等待队列的任务执行结束。
  • 任务按顺序执行的。按顺序执行的原因:虽然并发队列可以开启多个线程,并且同时执行多个任务。但是因为本身不能创建新线程,只有当前线程这一个线程(同步任务不具备开启新线程的能力),所以也就不存在并发。而且当前线程只有等待当前队列中正在执行的任务执行完毕之后,才能继续接着执行下面的操作(同步任务需要等待队列的任务执行结束)。所以任务只能一个接一个按顺序执行,不能同时被执行

异步 + 串行队列

- (void)asyncSerialQueue {
    // 开启新线程,串行,执行完成后再执行下一个任务
    NSLog(@"sync serial---begin,%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.aSerial", DISPATCH_QUEUE_SERIAL);
    // 任务1
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    NSLog(@"sync serial---end,%@", [NSThread currentThread]);
}

运行结果:

2019-05-23 10:18:11.267306+0800 GCD[662:121472] sync serial---begin,<NSThread: 0x17406ac80>{number = 1, name = main}
2019-05-23 10:18:11.267486+0800 GCD[662:121472] sync serial---end,<NSThread: 0x17406ac80>{number = 1, name = main}
2019-05-23 10:18:13.273644+0800 GCD[662:121510] 任务1 sync serial for:0 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}
2019-05-23 10:18:15.279136+0800 GCD[662:121510] 任务1 sync serial for:1 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}
2019-05-23 10:18:17.284570+0800 GCD[662:121510] 任务1 sync serial for:2 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}
2019-05-23 10:18:19.289004+0800 GCD[662:121510] 任务2 sync serial for:0 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}
2019-05-23 10:18:21.294598+0800 GCD[662:121510] 任务2 sync serial for:1 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}
2019-05-23 10:18:23.300166+0800 GCD[662:121510] 任务2 sync serial for:2 queue:<NSThread: 0x170073a40>{number = 3, name = (null)}

分析如下:

  • 开启了一条新线程(异步执行具备开启新线程的能力,串行队列只开启一个线程)。
  • 异步执行不会做任何等待,可以继续执行任务
  • 任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。

异步 + 并发队列

- (void)asyncConcurrentQueue {
    // 开启多个线程,任务交替(同时)执行
    NSLog(@"async Concurrent---begin,%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.aConcurrent", DISPATCH_QUEUE_CONCURRENT);
    // 任务1
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    NSLog(@"async Concurrent---end,%@", [NSThread currentThread]);
}

运行结果:

2019-05-23 10:18:49.427548+0800 GCD[664:121763] sync serial---begin,<NSThread: 0x174075380>{number = 1, name = main}
2019-05-23 10:18:49.427720+0800 GCD[664:121763] sync serial---end,<NSThread: 0x174075380>{number = 1, name = main}
2019-05-23 10:18:51.433513+0800 GCD[664:121808] 任务1 sync serial for:0 queue:<NSThread: 0x17007da00>{number = 3, name = (null)}
2019-05-23 10:18:51.433808+0800 GCD[664:121809] 任务2 sync serial for:0 queue:<NSThread: 0x170261a40>{number = 4, name = (null)}
2019-05-23 10:18:53.439079+0800 GCD[664:121808] 任务1 sync serial for:1 queue:<NSThread: 0x17007da00>{number = 3, name = (null)}
2019-05-23 10:18:53.439370+0800 GCD[664:121809] 任务2 sync serial for:1 queue:<NSThread: 0x170261a40>{number = 4, name = (null)}
2019-05-23 10:18:55.444643+0800 GCD[664:121808] 任务1 sync serial for:2 queue:<NSThread: 0x17007da00>{number = 3, name = (null)}
2019-05-23 10:18:55.444949+0800 GCD[664:121809] 任务2 sync serial for:2 queue:<NSThread: 0x170261a40>{number = 4, name = (null)}

分析如下:

  • 除了当前线程(主线程),系统又开启了2个线程,并且任务是交替/同时执行的。(异步执行具备开启新线程的能力。且并发队列可开启多个线程,同时执行多个任务)。
  • 所有任务是在打印的async Concurrent---beginasync Concurrent---end之后才执行的。说明当前线程没有等待,而是直接开启了新线程,在新线程中执行任务(异步执行不做等待,可以继续执行任务)。

异步并发 + 统一回调

这个就比较有意思了,经常需要并发执行然后统一回调到主线程刷新UI。这个就可以多钟方式实现,不是单一的方式来处理。

队列组1

// 异步并发 + 统一回调 Queue Group
- (void)asyncConcurrentAfterUnifiedCallbackQueueGroup {
    // 信号方式
    NSLog(@"queue Group---begin,%@", [NSThread currentThread]);
    dispatch_group_t group =  dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 任务1
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 方式一:等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程)
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);


    // 方式二:等前面的异步任务1、任务2都执行完毕后,回到主线程执行下边任务
//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//        NSLog(@"enter main queue:%@", [NSThread currentThread]);
//    });

    NSLog(@"queue Group---end,%@", [NSThread currentThread]);
}

打印结果:

2019-05-23 10:21:20.008168+0800 GCD[667:122358] queue Group---begin,<NSThread: 0x170079240>{number = 1, name = main}
2019-05-23 10:21:22.013825+0800 GCD[667:122396] 任务1 sync serial for:0 queue:<NSThread: 0x170267f80>{number = 3, name = (null)}
2019-05-23 10:21:22.014138+0800 GCD[667:122399] 任务2 sync serial for:0 queue:<NSThread: 0x170267fc0>{number = 4, name = (null)}
2019-05-23 10:21:24.019413+0800 GCD[667:122396] 任务1 sync serial for:1 queue:<NSThread: 0x170267f80>{number = 3, name = (null)}
2019-05-23 10:21:24.019699+0800 GCD[667:122399] 任务2 sync serial for:1 queue:<NSThread: 0x170267fc0>{number = 4, name = (null)}
2019-05-23 10:21:26.024980+0800 GCD[667:122396] 任务1 sync serial for:2 queue:<NSThread: 0x170267f80>{number = 3, name = (null)}
2019-05-23 10:21:26.025288+0800 GCD[667:122399] 任务2 sync serial for:2 queue:<NSThread: 0x170267fc0>{number = 4, name = (null)}
2019-05-23 10:21:26.025478+0800 GCD[667:122358] queue Group---end,<NSThread: 0x170079240>{number = 1, name = main}

分析如下:

  • 方式二:dispatch_group_wait等待上面的任务全部完成后,会往下继续执行(会阻塞当前线程),dispatch_group_wait之后的如果不需要再增加任务,可以直接操作主线程的UI更新。
  • 方式二:dispatch_group_notify会它之前的任务都执行完成后才执行dispatch_group_notify里面的任务,可以直接指定回到主线程执行任务。
  • Queue Group---end最后执行因为通过dispatch_group_wait方式来给线程加锁,所以需要等待线程任务执行后才能执行。当然如果是用dispatch_group_notify情况就不一样了,不需要等待就可以依次执行了。

队列组2

- (void)asyncConcurrentAfterUnifiedCallbackQueueGroupOther {
    // 信号方式
    NSLog(@"Queue Group---begin,%@", [NSThread currentThread]);
    dispatch_group_t group =  dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.aConcurrent", DISPATCH_QUEUE_CONCURRENT);
    // 任务1
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    // 任务2
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
        dispatch_group_leave(group);
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"enter main queue:%@", [NSThread currentThread]);
    });
    NSLog(@"Queue Group---end,%@", [NSThread currentThread]);
}

打印结果:

2019-05-23 10:22:59.156522+0800 GCD[669:122790] Queue Group---begin,<NSThread: 0x17407cf40>{number = 1, name = main}
2019-05-23 10:22:59.156737+0800 GCD[669:122790] Queue Group---end,<NSThread: 0x17407cf40>{number = 1, name = main}
2019-05-23 10:23:01.162977+0800 GCD[669:122832] 任务1 sync serial for:0 queue:<NSThread: 0x17026dc00>{number = 4, name = (null)}
2019-05-23 10:23:01.162976+0800 GCD[669:122834] 任务2 sync serial for:0 queue:<NSThread: 0x174260e80>{number = 3, name = (null)}
2019-05-23 10:23:03.169392+0800 GCD[669:122832] 任务1 sync serial for:1 queue:<NSThread: 0x17026dc00>{number = 4, name = (null)}
2019-05-23 10:23:03.169664+0800 GCD[669:122834] 任务2 sync serial for:1 queue:<NSThread: 0x174260e80>{number = 3, name = (null)}
2019-05-23 10:23:05.174832+0800 GCD[669:122832] 任务1 sync serial for:2 queue:<NSThread: 0x17026dc00>{number = 4, name = (null)}
2019-05-23 10:23:05.175196+0800 GCD[669:122834] 任务2 sync serial for:2 queue:<NSThread: 0x174260e80>{number = 3, name = (null)}
2019-05-23 10:23:05.175455+0800 GCD[669:122790] enter main queue:<NSThread: 0x17407cf40>{number = 1, name = main}

分析如下:

  • 通过dispatch_group_createdispatch_group_enterdispatch_group_leave来确保任务已经被执行,等标记的任务执行完成后再执行dispatch_group_notify里面的任务(可直接切换回主线程)。
  • Queue Group---beginQueue Group---end不会等待线程执行完成后,因为是并发则无需等待所以会直接执行。

信号semaphore

- (void)asyncConcurrentAfterUnifiedCallbackSemaphore {
    // 信号方式
    NSLog(@"sync serial---begin,%@", [NSThread currentThread]);
    self.semaphore = dispatch_semaphore_create(1);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.aConcurrent", DISPATCH_QUEUE_CONCURRENT);

    // 任务1
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
        dispatch_semaphore_signal(self.semaphore);
    });
    // 任务2
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
        dispatch_semaphore_signal(self.semaphore);
    });
    // 任务3
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务3 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
    // 进入主线程

    NSLog(@"sync serial---end,%@", [NSThread currentThread]);
    /*
        按顺序执行
    */
}

打印结果:

2019-05-23 10:23:50.745169+0800 GCD[672:123178] sync serial---begin,<NSThread: 0x17406fac0>{number = 1, name = main}
2019-05-23 10:23:52.750928+0800 GCD[672:123202] 任务1 sync serial for:0 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:23:54.756484+0800 GCD[672:123202] 任务1 sync serial for:1 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:23:56.762050+0800 GCD[672:123202] 任务1 sync serial for:2 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:23:58.767683+0800 GCD[672:123202] 任务2 sync serial for:0 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:23:58.768051+0800 GCD[672:123206] 任务3 sync serial for:0 queue:<NSThread: 0x17006f300>{number = 4, name = (null)}
2019-05-23 10:24:00.774134+0800 GCD[672:123202] 任务2 sync serial for:1 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:24:00.774389+0800 GCD[672:123206] 任务3 sync serial for:1 queue:<NSThread: 0x17006f300>{number = 4, name = (null)}
2019-05-23 10:24:02.779683+0800 GCD[672:123202] 任务2 sync serial for:2 queue:<NSThread: 0x170079780>{number = 3, name = (null)}
2019-05-23 10:24:02.779952+0800 GCD[672:123178] sync serial---end,<NSThread: 0x17406fac0>{number = 1, name = main}
2019-05-23 10:24:02.790396+0800 GCD[672:123206] 任务3 sync serial for:2 queue:<NSThread: 0x17006f300>{number = 4, name = (null)}

为了能够统一回调也可以通过信号的方式来标记,然后dispatch_semaphore_wait等待直到符合跳进才往下执行。

栅栏Barrier

- (void)asyncConcurrentAfterUnifiedCallbackBarrier {
    // 栅栏方式
    NSLog(@"sync serial---begin,%@", [NSThread currentThread]);
    dispatch_queue_t queue = dispatch_queue_create("net.gcd.aConcurrent", DISPATCH_QUEUE_CONCURRENT);
    // 任务1
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"任务1 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 任务2
    dispatch_async(queue, ^{
        for (int i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:1];
            NSLog(@"任务2 sync serial for:%d queue:%@", i, [NSThread currentThread]);
        }
    });
    // 栅栏
//    dispatch_barrier_sync(queue, ^{
//        // 同步则进入主线程
//        NSLog(@"sync enter barrier:%@", [NSThread currentThread]);
//    });
    dispatch_barrier_async(queue, ^{
        // 异步栅栏,子线程执行
        NSLog(@"async enter barrier:%@", [NSThread currentThread]);
        // 进入主线程
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"enter main queue:%@", [NSThread currentThread]);
        });
    });

    NSLog(@"sync serial---end,%@", [NSThread currentThread]);
}

打印结果:

2019-05-23 10:25:27.469154+0800 GCD[674:123595] sync serial---begin,<NSThread: 0x1700780c0>{number = 1, name = main}
2019-05-23 10:25:27.469338+0800 GCD[674:123595] sync serial---end,<NSThread: 0x1700780c0>{number = 1, name = main}
2019-05-23 10:25:28.471259+0800 GCD[674:123637] 任务2 sync serial for:0 queue:<NSThread: 0x17407a740>{number = 3, name = (null)}
2019-05-23 10:25:29.475087+0800 GCD[674:123640] 任务1 sync serial for:0 queue:<NSThread: 0x170261d00>{number = 4, name = (null)}
2019-05-23 10:25:29.476803+0800 GCD[674:123637] 任务2 sync serial for:1 queue:<NSThread: 0x17407a740>{number = 3, name = (null)}
2019-05-23 10:25:30.482237+0800 GCD[674:123637] 任务2 sync serial for:2 queue:<NSThread: 0x17407a740>{number = 3, name = (null)}
2019-05-23 10:25:31.480673+0800 GCD[674:123640] 任务1 sync serial for:1 queue:<NSThread: 0x170261d00>{number = 4, name = (null)}
2019-05-23 10:25:33.486150+0800 GCD[674:123640] 任务1 sync serial for:2 queue:<NSThread: 0x170261d00>{number = 4, name = (null)}
2019-05-23 10:25:33.486460+0800 GCD[674:123640] async enter barrier:<NSThread: 0x170261d00>{number = 4, name = (null)}
2019-05-23 10:25:33.486819+0800 GCD[674:123595] enter main queue:<NSThread: 0x1700780c0>{number = 1, name = main}

分析如下:

  • dispatch_barrier_sync直接进入主线程
  • dispatch_barrier_async在当前线程执行,无需等待,但是需要刷新UI的时候需要自己手动进入主线程。

关于统一回调

通过以上方式都能实现统一回调的效果,这个在实际开发中还是经常需要用到的,比如多条协议并发执行,但是需要等待所有的数据都加载完成后才能统一刷新UI,这个时候就需要使用线程来处理了。

总结

这边就大概说明这些吧。算是比较基础的,但也很实用,项目中还是比较经常用到的。后续如果觉得本文还有需要补充的会继续补充,先这样。

相关文章

  • GCD整理

    系统提供的dispatch方法: 为了方便地使用 GCD,苹果提供了一些方法方便我们将 block 放在主线程 或...

  • GCD整理

    这边就整理下GCD一些常用的方式,以及举例子并打印结果说明相应的。一直想去整理然后嘛,要么没时间要么懒...拖到现...

  • GCD整理

    基本的数据结构 GCD的类都是struct定义的。 包括所有的父类的数据成员,都平铺重复的写在一个个的struct...

  • iOS多线程之GCD详解

    原文链接:整理多线程:GCD详解,如有侵权立即删除 一、GCD简介 为什么要用GCD呢? 二、GCD任务和队列 2...

  • 【Objective-C】GCD介绍

    整理自raywenderlich。 1.GCD是嘛? GCD是Grand Central Dispatch的缩写,...

  • GCD整理(一)

    整理一篇关于GCD的文章,自己以后要复习的时候也方便。 GCD(Grand Center Dispatch)异步执...

  • GCD整理(二)

    这篇会整理GCD常用的API 目录1、dispatch_after2、dispatch_apply3、dispat...

  • GCD 简单整理

    GCD (Grand Central Dispatch) 概念 关注两个概念:队列、任务。iOS 多线程方案:pt...

  • 笔记整理:GCD

    一、概述 GCD是用纯C编写,效率很高;其内部自动维护一个线程池,自动管理线程的生命周期;其会利用CPU的多核特性...

  • 多线程之GCD学习整理

    把GCD的资料重新整理了一下 ( ̄. ̄) GCD全称Grand Central Dispatch是Apple开发的...

网友评论

      本文标题:GCD整理

      本文链接:https://www.haomeiwen.com/subject/bqsvzqtx.html