iOS 中 GCD 的使用

  1. 同步
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    });

  2. 异步
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    });

  3. 串行 DISPATCH_QUEUE_SERIAL

  4. 并行 DISPATCH_QUEUE_SERIAL_INACTIVE

  5. 主队列 dispatch_get_main_queue
    全局队列 dispatch_get_global_queue
    创建队列 dispatch_queue_create("w", DISPATCH_QUEUE_SERIAL);

  6. 延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    });

  7. 循环
    size_t size = 3;
    dispatch_apply(size, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t s) {

     NSLog(@"%zu", s);
    

    });

  8. 组队列 dispatch_group_t
    self.queue = dispatch_queue_create("com.chenzhou.queue", DISPATCH_QUEUE_SERIAL);

    dispatch_group_t myGruop = dispatch_group_create();
    dispatch_group_async(myGruop, self.queue, ^{

     sleep(2);
    

    });

    dispatch_group_async(myGruop, self.queue, ^{

     sleep(2);
    

    });
    // 异步函数 等待组加载完毕
    dispatch_group_notify(myGruop, dispatch_get_main_queue(), ^{
    self.sender.titleLabel.text = @"完成";
    });
    // 同步
    dispatch_group_wait(myGruop, DISPATCH_TIME_FOREVER);

    // 暂停 暂停后面添加的队列,前面已经加入不进入暂停
    dispatch_suspend(self.queue);

    // 继续
    dispatch_resume(self.queue);

你可能感兴趣的:(iOS 中 GCD 的使用)