Begin iOS7 GCD

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.