dispatch_async 和dispatch_sync

原创
2015/03/31 15:17
阅读数 79
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 100, 100);
    button.backgroundColor = [UIColor brownColor];
    [self.view addSubview:button];
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{
        NSLog(@"111111111111111 %@",[NSThread currentThread]);
        __block UIImage *image = nil;
        dispatch_sync(concurrentQueue, ^{  //@1dispatch_sync
            NSLog(@"22222222222222 %@",[NSThread currentThread]);
            //download image
            NSString *urlString = @"http://images.apple.com/v/macbook-pro/f/images/loupe.png";
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
            NSError *downloadError = nil;
            NSData *imageData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];
            if (downloadError == nil && imageData != nil) {
                image = [UIImage imageWithData:imageData];
            }else if(downloadError != nil){
                NSLog(@"Error Happened = %@",downloadError);
            }else{
                NSLog(@"No data could get download from URL.");
            }
        });
        dispatch_sync(concurrentQueue, ^{   //@2//dispatch_sync
            //show image here
            NSLog(@"33333333333 %@",[NSThread currentThread]);
            if (image != nil) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
                [imageView setImage:image];
               // [imageView setContentMode:UIViewContentModeScaleAspectFill];
                [self.view addSubview:imageView];
            }else{
                NSLog(@"Image isn't downloaded. Nothing to display.");
            }
        });
    });
}
@end

输出如下:

2015-03-31 15:13:59.848 iOSCookbook 7.7[21442:599814] 111111111111111 <NSThread: 0x7ff57ae02480>{number = 2, name = (null)}
2015-03-31 15:13:59.851 iOSCookbook 7.7[21442:599814] 22222222222222 <NSThread: 0x7ff57ae02480>{number = 2, name = (null)}
2015-03-31 15:13:59.992 iOSCookbook 7.7[21442:599814] 33333333333 <NSThread: 0x7ff57ae02480>{number = 2, name = (null)}


第二种测试:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 100, 100);
    button.backgroundColor = [UIColor brownColor];
    [self.view addSubview:button];
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{
        NSLog(@"111111111111111 %@",[NSThread currentThread]);
        __block UIImage *image = nil;
        dispatch_async(concurrentQueue, ^{  //@1dispatch_async
            NSLog(@"22222222222222 %@",[NSThread currentThread]);
            //download image
            NSString *urlString = @"http://images.apple.com/v/macbook-pro/f/images/loupe.png";
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
            NSError *downloadError = nil;
            NSData *imageData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];
            if (downloadError == nil && imageData != nil) {
                image = [UIImage imageWithData:imageData];
            }else if(downloadError != nil){
                NSLog(@"Error Happened = %@",downloadError);
            }else{
                NSLog(@"No data could get download from URL.");
            }
        });
        dispatch_sync(concurrentQueue, ^{   //@2//dispatch_sync
            //show image here
            NSLog(@"33333333333 %@",[NSThread currentThread]);
            if (image != nil) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
                [imageView setImage:image];
               // [imageView setContentMode:UIViewContentModeScaleAspectFill];
                [self.view addSubview:imageView];
            }else{
                NSLog(@"Image isn't downloaded. Nothing to display.");
            }
        });
    });
}
@end



将1@处改为dispatch_async

输出如下:

2015-03-31 15:16:13.143 iOSCookbook 7.7[21471:600996] 111111111111111 <NSThread: 0x7f8769f01670>{number = 2, name = (null)}
2015-03-31 15:16:13.145 iOSCookbook 7.7[21471:600996] 33333333333 <NSThread: 0x7f8769f01670>{number = 2, name = (null)}
2015-03-31 15:16:13.145 iOSCookbook 7.7[21471:600996] Image isn't downloaded. Nothing to display.
2015-03-31 15:16:13.145 iOSCookbook 7.7[21471:600997] 22222222222222 <NSThread: 0x7f8769e29ee0>{number = 3, name = (null)}

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 100, 100);
    button.backgroundColor = [UIColor brownColor];
    [self.view addSubview:button];
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{
        NSLog(@"111111111111111 %@",[NSThread currentThread]);
        __block UIImage *image = nil;
        dispatch_sync(concurrentQueue, ^{  //@1dispatch_sync
            NSLog(@"22222222222222 %@",[NSThread currentThread]);
            //download image
            NSString *urlString = @"http://images.apple.com/v/macbook-pro/f/images/loupe.png";
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
            NSError *downloadError = nil;
            NSData *imageData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&downloadError];
            if (downloadError == nil && imageData != nil) {
                image = [UIImage imageWithData:imageData];
            }else if(downloadError != nil){
                NSLog(@"Error Happened = %@",downloadError);
            }else{
                NSLog(@"No data could get download from URL.");
            }
        });
        dispatch_sync(dispatch_get_main_queue(), ^{   //@2//dispatch_sync
         // dispatch_get_main_queue() 
         //show image here
            NSLog(@"33333333333 %@",[NSThread currentThread]);
            if (image != nil) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
                [imageView setImage:image];
               // [imageView setContentMode:UIViewContentModeScaleAspectFill];
                [self.view addSubview:imageView];
            }else{
                NSLog(@"Image isn't downloaded. Nothing to display.");
            }
        });
    });
}
@end

不知道为什么使用dispatch_get_main_queue() 比concurrentQueue要快很多!


一般的使用原则如下:

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



展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部