iOS Learning Notes (8) A preliminary study of--GCD

Source: Internet
Author: User

1. APPDELEGATE.M

1 #import "AppDelegate.h"2 #import "ViewController.h"3 @interfaceappdelegate ()4 5 @end6 7 @implementationappdelegate8 9 Ten-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions { One     //Override point for customization after application launch. ASelf.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; -Self.window.rootViewController = [[Viewcontroller alloc] Initwithnibname:@"Slowworderview"Bundle:nil]; -Self.window.backgroundColor =[Uicolor Whitecolor]; the [Self.window makekeyandvisible]; -     returnYES; - } -  +- (void) Applicationwillresignactive: (UIApplication *) Application { -     //Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. +     //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. A } at  -- (void) Applicationdidenterbackground: (UIApplication *) Application { -     //Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in Formation to the restore your application to the it is terminated later. -     //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits. - } -  in- (void) Applicationwillenterforeground: (UIApplication *) Application { -     //Called as part of the transition from the background to the inactive state; Here you can undo many of the changes Ma De on entering the background. to } +  -- (void) Applicationdidbecomeactive: (UIApplication *) Application { the     //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface. * } $ Panax Notoginseng- (void) Applicationwillterminate: (UIApplication *) Application { -     //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:. the } +  A @end

2. ViewController.h

1 #import <UIKit/UIKit.h>23@interface  viewcontroller: Uiviewcontroller45 @property (weak, nonatomic) iboutlet UIButton *Startbutton; 6 @property (weak, nonatomic) iboutlet Uitextview *Resultstextview; 7 8 @end

3. viewcontroller.m

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 5 @end6 7 @implementationViewcontroller8 9- (void) Viewdidload {Ten [Super Viewdidload]; One     //additional setup after loading the view, typically from a nib. A } -  -- (void) didreceivememorywarning { the [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. - } -  +-(NSString *) Fetchsomethingfromserver { -[Nsthread Sleepfortimeinterval:1]; +Nsurl *url = [Nsurl urlwithstring:@"http://nycode.sinaapp.com/d.php"]; ANserror *error =Nil; at      -Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; -NSData *response =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; -Nsdictionary *arr = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves error:&ERROR]; -     return[Arr Objectforkey:@"name"]; - } in  --(NSString *) ProcessData: (NSString *) Data { to[Nsthread Sleepfortimeinterval:2]; +     return[Data uppercasestring]; - } the  *-(NSString *) Calculatefirstresult: (NSString *) Data { $[Nsthread Sleepfortimeinterval:3];Panax Notoginseng     return[NSString stringWithFormat:@"Number of chars%lu", [data length]]; - } the  +-(NSString *) Calculatesecondresult: (NSString *) Data { A[Nsthread Sleepfortimeinterval:4]; the     return[Data stringbyreplacingoccurrencesofstring:@"E"Withstring:@"e"]; + } -  $-(Ibaction) DoWork: (ID) Sender { $NSDate *starttime =[NSDate Date]; -NSString *fetchedata =[self fetchsomethingfromserver]; -NSString *processddata =[self processdata:fetchedata]; theNSString *firstresult =[self calculatefirstresult:processddata]; -NSString *secondresult =[self calculatesecondresult:processddata];WuyiNSString *resultssummary = [NSString stringWithFormat:@"first:[%@]\tsecond:[%@]", Firstresult, Secondresult]; theSelf.resultsTextView.text =resultssummary; -NSDate *endtime =[NSDate Date]; WuNSLog (@"Complete in %f seconds", [EndTime timeintervalsincedate:starttime]); - } About  $ @end

4. xib file put a TextView and button and correlate outputs and methods

5. Summary

After the application starts, the program runs for 10 seconds.

The console displays the elapsed time interval

iOS Learning Notes (8) A preliminary study of--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.