GCD

作者: March_Cullen | 来源:发表于2017-03-03 15:33 被阅读0次

    1、dispatch_async

    为了避免程序在处理耗时的操作时界面卡死,如读取网络数据、IO、数据库读写等,我们会开启一个新的线程处理这些操作,然后通知主线程刷新UI。用GCD实现实现,代码框架如下,

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
        // 耗时的操作  
        dispatch_async(dispatch_get_main_queue(), ^{  
            // 更新界面  
        });  
    });  
    

    例如:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
        NSURL * url = [NSURL URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"];  
        NSData * data = [[NSData alloc]initWithContentsOfURL:url];  
        UIImage *image = [[UIImage alloc]initWithData:data];  
        if (data != nil) {  
            dispatch_async(dispatch_get_main_queue(), ^{  
                self.imageView.image = image;  
             });  
        }  
    }); 
    

    2、dispatch_group_async

    dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。比如现在有三个下载任务,当三个任务都下载完成后才通知界面刷新UI。代码如下,

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
    dispatch_group_t group = dispatch_group_create();  
    
    dispatch_group_async(group, queue, ^{  
        [NSThread sleepForTimeInterval:1];  
        NSLog(@"group1");  
    });  
    
    dispatch_group_async(group, queue, ^{  
        [NSThread sleepForTimeInterval:2];  
        NSLog(@"group2");  
    });  
    
    dispatch_group_async(group, queue, ^{  
        [NSThread sleepForTimeInterval:3];  
        NSLog(@"group3");  
    });  
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{  
        NSLog(@"refreshUI");  
    });  
    
    dispatch_release(group);  
    

    dispatch_group_async是异步的方法,运行后可以看到打印结果:

    2017-03-02 15:23:23.627 GCDTest[32327:18237] group1
    2017-03-02 15:23:24.626 GCDTest[32327:19c6b] group2
    2017-03-02 15:23:25.626 GCDTest[32327:20236] group3
    2017-03-02 15:23:26.626 GCDTest[32327:e347] refreshUI
    

    3、dispatch_barrier_async
    dispatch_barrier_async的作用是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行。barrier,栅栏的意思。如:

    dispatch_queue_t queue = dispatch_queue_create("diapatchQueue", DISPATCH_QUEUE_CONCURRENT); 
    
    dispatch_async(queue, ^{  
        [NSThread sleepForTimeInterval:2];  
        NSLog(@"dispatch_async1");  
    });  
    
    dispatch_async(queue, ^{  
        [NSThread sleepForTimeInterval:4];  
        NSLog(@"dispatch_async2");  
    });  
    
    dispatch_barrier_async(queue, ^{  
        NSLog(@"dispatch_barrier_async");  
        [NSThread sleepForTimeInterval:4];  
    });  
    
    dispatch_async(queue, ^{  
        [NSThread sleepForTimeInterval:1];  
        NSLog(@"dispatch_async3");  
    });  
    

    打印结果如下:

    2017-03-02 15:25:23.627 GCDTest[32327:11391] dispatch_async1
    
    2017-03-02 15:25:25.627 GCDTest[32327:11391] dispatch_async2
    
    2017-03-02 15:25:25.627 GCDTest[32327:11391] dispatch_barrier_async
    
    2017-03-02 15:25:30.627 GCDTest[32327:11391] dispatch_async3
    

    相关文章

      网友评论

          本文标题:GCD

          本文链接:https://www.haomeiwen.com/subject/pycrgttx.html