GCD整理

这边就整理下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,这个时候就需要使用线程来处理了。

总结

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容