• dispatch_async 与 dispatch_get_global_queue


    #define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)


    - (void)viewDidLoad

    {

        [super viewDidLoad];

        dispatch_async(kBgQueue, ^{

            NSData* data = [NSData dataWithContentsOfURLkLatestKivaLoansURL];

            [self performSelectorOnMainThread:@selector(fetchedData:) withObject:datawaitUntilDone:YES];

        });

    }


    dispatch_async会向kBgQueue队列中添加新的任务去执行,这里kBgQueue队列使用dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)获得。


    Dispatch Queues

    Dispatch Queues从使用的角度将更象另一种形式的Operation Queues只是 Operation Queuse是用ObjectC的Dispatch Queues是C的

    dispatch Queues有serial Queues 也被称为私有dispatch Queues,一个时间只能运行一个task,顺序运行

    dispatch_queue_t queue;
    queue = dispatch_queue_create("myQueue", NULL);  

    dispatch_async(queue, ^{
            printf("Do some work here.\n");
        });
        printf("The first block may or may not have run.\n");
        dispatch_sync(queue, ^{
            printf("Do some more work here.\n");
        });
        printf("Both blocks have completed.\n");

    这里使用了同步dispatch和异步dispatch,推荐使用dispatch_async这样才能真正体现其中的优势同步相当于WaitUntil = YES

     

     

    还有一种就是Concurrent Queues每个程序系统自动提供了3个Concurrent Queues

    dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_queue_t aHQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
     dispatch_queue_t aLQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);

    啥意思一看就明白,3个优先级别的concurrent queues

     

    最后一个特殊的Dispatch Queue就是main dispatch Queue 也是程序启动自动生成

    dispatch_queue_t mainQueue = dispatch_get_main_queue();

     

    concurrent queues和main queue 都是由系统生成而且 dispatch_suspend, dispatch_resume, dispatch_set_context,这些函数对他们无效

     

    但是我们的应用不是简单的同步也异步的运行,应用经常是混合的

    比如我们要task1 task2 task3 都运行完成后才能异步运行task4 task5 task6我们该怎么做呢?这里我们可以引入group的概念

     

        dispatch_queue_t aDQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
        // Add a task to the group
        dispatch_group_async(group, aDQueue, ^{
            printf("task 1 \n");
        });
        dispatch_group_async(group, aDQueue, ^{
            printf("task 2 \n");
        });
        dispatch_group_async(group, aDQueue, ^{
            printf("task 3 \n");
        });
        printf("wait 1 2 3 \n");
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        printf("task 1 2 3 finished \n");
        dispatch_release(group);
        group = dispatch_group_create();
        // Add a task to the group
        dispatch_group_async(group, aDQueue, ^{
            printf("task 4 \n");
        });
        dispatch_group_async(group, aDQueue, ^{
            printf("task 5 \n");
        });
        dispatch_group_async(group, aDQueue, ^{
            printf("task 6 \n");
        });
        printf("wait 4 5 6 \n");
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        printf("task 4 5 6 finished \n");
        dispatch_release(group);

     

    有时候我们也可以将设定一个数据在queue中 也可以定义一个结束函数

    dispatch_set_finalizer_f 是在dispatch_release时候被调用

        dispatch_queue_t serialQueue = dispatch_queue_create("com.example.CriticalTaskQueue", NULL);
        if (serialQueue)
        {
            dispatch_set_context(serialQueue, self);
            dispatch_set_finalizer_f(serialQueue, &myFinalizerFunction);
        }
        
        dispatch_group_t group = dispatch_group_create();
        
        // Add a task to the group
        dispatch_group_async(group, serialQueue, ^{
            printf("task 1 \n");
        });
        
        dispatch_group_async(group, serialQueue, ^{
            printf("task 2 \n");
        });
        
        dispatch_group_async(group, serialQueue, ^{
            printf("task 3 \n");
        });
        printf("wait 1 2 3 \n");
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);  
        dispatch_release(group);
        dispatch_release(serialQueue);


    欢迎光临小站 好岸园 http://www.hopean.com
  • 相关阅读:
    洛谷P1880 [NOI1995]石子合并 (区间dp
    洛谷P1012 拼数(水题 字符串
    洛谷P1071 潜伏者(水题
    微信《跳一跳》超高分攻略,轻松排行榜首
    StringUtilsd的isEmpty、isNotEmpty、isBlank、isNotBlank
    java.util.Properties类
    timer.scheduleAtFixedRate和timer.schedule的实验
    schedule() 和 scheduleAtFixedRate() 区别
    session和jsessionid有什么关系
    增加Xss过滤步骤
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/3047035.html
Copyright © 2020-2023  润新知