任务 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 dataWithContentsOfURL: kLatestKivaLoansURL];

        [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);





- (void)checkUpdate

{

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        // Do a taks in the background

        NSDictionary *dic = [iKnowAPI checkUpdate];

        

        // Hide the HUD in the main tread 

        dispatch_async(dispatch_get_main_queue(), ^{

            if (dic && ![[dic objectForKey:@"necessary"] isEqualToString:@"-1"]) 

            {

                NSString *message = [dic objectForKey:@"des"];

                message = [message length] ? message : NSLocalizedString(@"当前有新版本可以更新", @"");

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"新版本提醒", @"")

                                                                    message:message 

                                                                   delegate:nil 

                                                          cancelButtonTitle:NSLocalizedString(@"知道了", @"")

                                                          otherButtonTitles:nil];

                

                [alertView show];

                [alertView release];

            }

        });

    });

}


你可能感兴趣的:(ios,object-c,dispatch_async)