IOS Multithreading GCD Detailed _ios

Source: Internet
Author: User
Tags call back gcd

Grand-Dispatch (GCD) is a solution for multi-core programming developed by Apple.

The dispatch queue is divided into the following three types:

1 runs the main thread of the main queue and gets it through the dispatch_get_main_queue.

#definedispatch_get_main_queue () \dispatch_global_object (dispatch_queue_t, _dispatch_main_q)

It can be seen that Dispatch_get_main_queue is also a kind of dispatch_queue_t.

2 Parallel Queue Global dispatch queue, obtained by Dispatch_get_global_queue, by the system to create three different priority dispatch queue. Parallel queues are executed in the same order as they were queued.

3 serial queue serial queues is typically used to synchronize access sequentially, creating any number of serial queues that are concurrent between each serial queue.

Serial queues are useful when you want tasks to be performed in a particular order. A serial queue performs only one task at a time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue can guarantee that a task executes in a predictable order.

Serial queues created by Dispatch_queue_create, you can use function Dispatch_retain and dispatch_release to increase or decrease the reference count.

Usage of GCD:

/Background execution: Dispatch_async (Dispatch_get_global_queue (0,0), ^{//something});

Main thread execution: Dispatch_async (Dispatch_get_main_queue (), ^{//something});

One-time execution: staticdispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{//code to be executed once});

Delay 2 sec 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 \ after delay});

Custom dispatch_queue_tdispatch_queue_t Urls_queue = Dispatch_queue_create ("blog.devtang.com", NULL); Dispatch_async (Urls_queue,^{//your code});

Dispatch_release (Urls_queue);

Merge rollup Results dispatch_group_t Group =dispatch_group_create ();

Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{//parallel execution of the thread one});

Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{//parallel thread Two});

Dispatch_group_notify (Group, Dispatch_get_global_queue (0,0), ^{//Rollup results}); 

An example of applying GCD:

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default,0), ^{

nsurl* url = [NSURL urlwithstring:@ "http://www.baidu.com"];

Nserror*error;

nsstring* data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];if (data!=nil) {

Dispatch_async (Dispatch_get_main_queue (), ^{

NSLog (@ "Call back, the data is:%@", data);

}

else{

NSLog (@ "error when download:%@", error);

}

);

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

When GCD is not used, the app has a maximum of 5 seconds to do some saving or cleanup work when the app is pulled out of the home key. 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 the local cache, send statistics, and so on.

The sample code to keep the program running in the background is as follows:

AppDelegate.h file @property (assign, nonatomic) uibackgroundtaskidentifier backgroundupdatetask;//appdelegate.m file-( void) Applicationdidenterbackground: (uiapplication *) application

{

[self beingbackgroundupdatetask];// Add here the code you need to run long [self endbackgroundupdatetask];

} -(void) beingbackgroundupdatetask

{

self.backgroundupdatetask= [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{

[self endbackgroundupdatetask];}

]

-(void) endbackgroundupdatetask

{

[[uiapplication sharedapplication] Endbackgroundtask: Self.backgroundupdatetask];

Self.backgroundupdatetask=uibackgroundtaskinvalid;

}

Text/Left hand (Jane book author)
Original link: HTTP://WWW.JIANSHU.COM/P/2C584601E3BF
Copyright belongs to the author, reproduced please contact the author to obtain authorization, and labeled "Jane book author."

The above is the iOS multithreading GCD data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Related Article

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.