IOS Development Multi-thread GCD

Source: Internet
Author: User

1.GCD (Grand Centrol Dispath)

Parallel: Both macro and micro are two people holding two shovels in digging pits, digging two big pits in an hour

Concurrency: The macro is feeling they are digging holes, microscopic is they are using a shovel digging pits, an hour after they dug two small pits.

Summary: In the case of a single CPU, most of the process is concurrent, is a spade, you just a moment, I only a short interval, the user can not feel it.


Application :

GCD includes:

(1) In actual use

Dispatch_get_global_queue (0, 0) The first 0 is the priority, the second reserved field
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
//Can be a data request here
nsstring* result = [self requestdata:parameter];
//Return to main thread here to refresh data
Dispatch_async (Dispatch_get_main_queue (), ^{
[Maintableview Reloaddata];
});
});

To illustrate:

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);
}
});


(2) You can create it yourself (I don't use it)

Serial queue, as the name implies, a bunch of it, it has to be executed concurrently

Create your own serial queue
dispatch_queue_t queue = dispatch_queue_create ("Com.class15.queue", dispatch_queue_serial);
Asynchronous execution Thread
Dispatch_async (Queue, ^{
NSLog (@ "task 1:%@%d", [Nsthread currentthread],[nsthread currentthread].ismainthread);
});



Parallel queues are obtained through Dispatch_get_global_queue, and the system creates three different priority dispatch queue

To create your own queue
dispatch_queue_t queue = dispatch_queue_create ("Com.class15.comcrrentQueue", dispatch_queue_concurrent);

Dispatch_async (Queue, ^{
NSLog (@ "task 1:%@%d", [Nsthread currentthread],[nsthread currentthread].ismainthread);
});

IOS Development Multi-thread 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.