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 three controls @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"];} Click Event-(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); Note that the string is defined only, does not allocate space for the string, the append string fails, and the correct way to define NULL here is __block nsstring *resultssummary = [NSString stringwithformat:@ ""]; asynchronous loading, which may not have been completed before the main thread is executedAsynchronous load 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 (), ^{//Here The string is still empty, because the asynchronous load execution statement is not finished yet here resultssummary = [Res Ultssummary stringbyappendingstring:@ "1111"]; The contents of the label are 1111, or other possible results 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]; Additional setup after loading the view, typically from a nib. [Self.spinner Sethidden:yes];} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Begin iOS7 GCD