Begin iOS7 GCD
// // ViewController.m // Begin iOS7 GCD // // Created by zyz on 15-4-1. // Copyright (c) 2015年 Apple. All rights reserved. // #import "ViewController.h" @interface ViewController () //storyboard三个控件 @property (weak, nonatomic) IBOutlet UIButton *startButton; @property (weak, nonatomic) IBOutlet UILabel *resultTextView; @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner; @end @implementation ViewController - (NSString *)fetchSomethingFromServer{ [NSThread sleepForTimeInterval:1]; return @"Hi, Here"; } - (NSString *)processData:(NSString *)data{ [NSThread sleepForTimeInterval:2]; return [data uppercaseString]; } - (NSString *)calculateFirstResult:(NSString *)data{ [NSThread sleepForTimeInterval:3]; return [NSString stringWithFormat:@"Number of chars: %lu", (unsigned long)[data length]]; } - (NSString *)calculateSecondResult:(NSString *)data{ [NSThread sleepForTimeInterval:4]; return [data stringByReplacingOccurrencesOfString:@"E" withString:@"e"]; } //点击事件 - (IBAction)buttonClick:(id)sender { self.startButton.enabled = NO; [self.spinner setHidden:NO]; [self.spinner startAnimating]; dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue1, ^{ NSLog(@"queue1 Thread is %@",[NSThread currentThread]); NSDate *startTime = [NSDate date]; NSString *fetchedDate = [self fetchSomethingFromServer]; NSString *processDate = [self processData:fetchedDate]; NSString *firstResult = [self calculateFirstResult:processDate]; NSString *secondResult = [self calculateSecondResult:processDate]; dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_queue_t queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //注意字符串只定义,不会为字符串分配空间,追加字符串会失败,这里定义为空的正确方式 __block NSString *resultsSummary = [NSString stringWithFormat:@""]; //异步加载,可能还没有执行完成就执行主线程的异步加载 dispatch_async(queue2, ^{ resultsSummary = [NSString stringWithFormat:@"First:[%@]\n",firstResult]; NSLog(@"queue2 Thread is %@",[NSThread currentThread]); }); dispatch_async(queue3, ^{ resultsSummary = [resultsSummary stringByAppendingString:[NSString stringWithFormat:@"Second:[%@]",secondResult]]; NSLog(@"queue3 Thread is %@",[NSThread currentThread]); }); dispatch_async(dispatch_get_main_queue(), ^{ //这里字符串仍然为空,因为异步加载执行的语句还没有完成就到了这里 resultsSummary = [resultsSummary stringByAppendingString:@"1111"]; //label的内容是1111,或者是其他的可能结果 self.resultTextView.text = resultsSummary; self.resultTextView.backgroundColor = [UIColor brownColor]; [self.spinner stopAnimating]; [self.spinner setHidden:YES]; }); NSDate *endTime = [NSDate date]; NSLog(@"Completed in %f seconds",[endTime timeIntervalSinceDate:startTime]); }); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.spinner setHidden:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。