GCD Common operations

Source: Internet
Author: User

GCD multithreaded Operations

1) The most used operation

Get Global Queue

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);

Asynchronous execution

Dispatch_async (Queue, ^{

Download image

Dispatch_async (Dispatch_get_main_queue (), ^{

Back to the main thread update UI

});

});

?

2) Delay execution 1-call nsobject method [self performselector: @seletor (Run) Withobject:nil afterdelay:2.0];2-gcd (better, without regenerating method)

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

NSLog (@ "2222");

});

3) Guarantee method is executed only once throughout the program

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

});

?

4) Group operations, multiple requests, request completion, and operation

Create a group,

dispatch_group_t group = Dispatch_group_create ();

Open a task download image 1

__block UIImage *image1 = nil;

Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

Image1 = [];

});

__block UIImage *image2 = nil;

Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{

Image2 = [];

});

Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{

Image1

Image2

});

GCD Common operations

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.