GCD Practical Summary
Image download
- Note: The utility of common GCD in iOS development is the same,
Let's take a look at the way we used to do it before:
1 StaticNsoperationqueue *queue;2 3-(Ibaction) Someclick: (ID) Sender {4Self.indicator.hidden =NO;5 [Self.indicator startanimating];6Queue =[[Nsoperationqueue alloc] init];7Nsinvocationoperation * op = [[[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (download)Object: nil] autorelease];8 [Queue Addoperation:op];9 }Ten One- (void) Download { ANsurl * url = [Nsurl urlwithstring:@"http://www.youdao.com"]; -Nserror *error; -NSString * data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&ERROR]; the if(Data! =Nil) { - [Self performselectoronmainthread: @selector (download_completed:) withobject:data Waituntildone:no]; -}Else { -NSLog (@"Error when download:%@", error); + [Queue release]; - } + } A at- (void) download_completed: (NSString *) Data { -NSLog (@" Call Back"); - [Self.indicator stopanimating]; -Self.indicator.hidden =YES; -Self.content.text =data; - [Queue release]; in}
After using GCD
If you use GCD, the above 3 methods can be put together as follows:
1 //The original code block one2Self.indicator.hidden =NO;3 [Self.indicator startanimating];4Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{5 //Original code block two6Nsurl * url = [Nsurl urlwithstring:@"http://www.youdao.com"];7Nserror *error;8NSString * data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&ERROR];9 if(Data! =Nil) {Ten //Original code block three OneDispatch_async (Dispatch_get_main_queue (), ^{ A [Self.indicator stopanimating]; -Self.indicator.hidden =YES; -Self.content.text =data; the }); -}Else { -NSLog (@"Error when download:%@", error); - } +});
First we can see that the code is getting shorter. Because there are fewer definitions of the original 3 methods, there are fewer packages of variables that need to be passed between each other.
Block has the following characteristics:
- The block can be defined inline in the code.
- The program block can access the available variables within the scope of the creation.
GCD everywhere
Dispatch methods provided by the system-most commonly used
To make it easy to use GCD, Apple provides some ways for us to put blocks on the main thread or background thread, or to postpone execution. Examples of use are as follows:
1 //Background execution:2Dispatch_async (Dispatch_get_global_queue (0,0), ^{3 //something4 });5 //main thread Execution:6Dispatch_async (Dispatch_get_main_queue (), ^{7 //something8 });9 //Disposable Execution:Ten Staticdispatch_once_t Oncetoken; OneDispatch_once (&oncetoken, ^{ A //code to be executed once - }); - //delay of 2 seconds execution: the 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 +});
running in the background
Another use of block is to allow the program to run longer in the background. In the past, when the app was left on the home button, the app only had a maximum of 5 seconds to do some saving or cleanup work. But the app can call UIApplication's Beginbackgroundtaskwithexpirationhandler method, allowing the app to run for up to 10 minutes long in the background. 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}
iOS Development--Multithreading OC Chapter &GCD Practical Summary