zoukankan      html  css  js  c++  java
  • GCD-调度组


    /*
    在iOS开发中,我们经常需要需要监听多个异步任务全部完成后的回调。此时需要用到调度组处理 场景:使用异步下载多张图片的时候,可能需要下载完成全部图片后才能计算image的高宽来布局页面。等等 */ #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; #warning 注意事项: /* 1.dispatch_group_enter(group) 与dispatch_group_leave(group) 必须配对使用!!! 2.如果 dispatch_group_leave(group) 配对少了一个,则dispatch_group_notify 将永远的等待下去不走回调 3.如果 dispatch_group_enter(group) 配对少了一个,则会造成程序的crash */ //1>创建一个调度组 dispatch_group_t group = dispatch_group_create(); //2.使用调度组 dispatch_group_enter(group); dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSLog(@"%@ --- 执行了任务1",[NSThread currentThread]); dispatch_group_leave(group); }); dispatch_group_enter(group); dispatch_async(dispatch_queue_create("111", DISPATCH_QUEUE_CONCURRENT), ^{ NSLog(@"%@ --- 执行了任务2",[NSThread currentThread]); dispatch_group_leave(group); }); //3>调度组监听,监听结果在设置的dispatch_get_main_queue 中回调 dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"%@ --- 监听到了任务全部执行完毕",[NSThread currentThread]); }); } @end

    代码下载地址:https://github.com/fushengit/iOS-Multithreading

     
  • 相关阅读:
    FAN_int2ExcelColChar functions
    How to enables AX email functionality without Outlook
    Global::validateEmail
    Global::time2StrHHMM_DNT
    Global::pickSpecificTable_DNT
    Global::pickSpecificClass_DNT
    Global::pickClassMethod_DNT
    Optimize date2str function
    How to achieve dialog with lookup control
    Optimize str2date function
  • 原文地址:https://www.cnblogs.com/fusheng-it/p/6930296.html
Copyright © 2011-2022 走看看