Multithreading GCD Usage

Source: Internet
Author: User
Tags gcd


1 //Background execution:2Dispatch_async (Dispatch_get_global_queue (0,0), ^{3 //something4 });5 6 //main thread Execution:7Dispatch_async (Dispatch_get_main_queue (), ^{8 //something9 });Ten One //Disposable Execution: A Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ - //code to be executed once the }); - - //delay of 2 seconds execution: - DoubleDelayinseconds =2.0; +dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, Delayinseconds *nsec_per_sec); -Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void){ + //Code to is executed on the main queue after delay A }); at - //Custom dispatch_queue_t -dispatch_queue_t Urls_queue = Dispatch_queue_create ("blog.yang99.com", NULL); -Dispatch_async (Urls_queue, ^{ -     //Your code - }); in dispatch_release (urls_queue);



to //Merge Summary Results +dispatch_group_t Group =dispatch_group_create (); -Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{ the //thread one in parallel execution * }); $Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{Panax Notoginseng //threads executed in parallel two - }); theDispatch_group_notify (Group, Dispatch_get_global_queue (0,0), ^{ + //Summary Results A});

Below is a core code that uses GCD background to download images and display them in the main thread:

1- (void) initializeuserinterface{2Self.indictorImageView.hidden =NO;3 [Self.indictorimageview startanimating];4Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{5Nsurl *url = [Nsurl urlwithstring:@"http://www.youdao.com"];6Nserror *error;7NSString *data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&ERROR];8         if(Data! =Nil) {9Dispatch_async (Dispatch_get_main_queue (), ^{Ten [Self.indictorimageview stopanimating]; OneSelf.indictorImageView.hidden =YES; ASelf.contentLabel.text = data;//Text Display -NSLog (@"data =%@", data); -[_webviewss Loadhtmlstring:data Baseurl:url];//Loading Web pages the             }); -}Else{ -NSLog (@"Error when download:%@", error); -         } +     }); -}

Another use of GCD is to allow programs to run longer in the background.

When you do not use GCD, when the app is left on the home button, the app only has a maximum of 5 seconds to do some saving or cleanup work. But after using GCD, the app has a maximum of 10 minutes to run in the background for a long time. This time can be used to clean up the local cache, send statistics and other work. The sample code that lets the program run long in the background is as follows:

1 //AppDelegate.h File2 @property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;3 4 //appdelegate.m File5- (void) Applicationdidenterbackground: (UIApplication *) Application6 {7 [self beingbackgroundupdatetask];8     //Add the code that you need to run long9 [self endbackgroundupdatetask];Ten } One  A- (void) Beingbackgroundupdatetask - { -Self.backgroundupdatetask = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{ the [self endbackgroundupdatetask]; -     }]; - } -  +- (void) Endbackgroundupdatetask - { + [[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask]; ASelf.backgroundupdatetask =Uibackgroundtaskinvalid; at}

Summary from: Tang Qi iOS Development Advanced

Multithreading GCD Usage

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.