GCD applications and Other methods

Source: Internet
Author: User
Tags gcd

1.GCD Application Single-case mode

Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{

NSLog (@ "Execute once%@", [Nsthread CurrentThread]);

});

2. Deferred operation
1.
NSLog (@ "Start execution");

[Self performselector: @selector (handleaction) Withobject:nil afterdelay:0];

2.

Dispatch_time_now from now on.
Delayinseconds delay for a few seconds

dispatch_time_t time = Dispatch_time (Dispatch_time_now, (int64_t) (2 * nsec_per_sec));

When dispatch_time_t

Dispatch_after (Time, Dispatch_get_main_queue (), ^{

NSLog (@ "%@", [Nsthread CurrentThread]);

});

3. Scheduling Group

Create a dispatch group
dispatch_group_t group = Dispatch_group_create ();
Get Global Queue
dispatch_queue_t queue = dispatch_get_global_queue (0, 0);
Asynchronous requests for dispatch groups
Dispatch_group_async (group, queue, ^{

[Nsthread Sleepfortimeinterval:2];
NSLog (@ "Download first picture");
});
Dispatch_group_async (group, queue, ^{
[Nsthread Sleepfortimeinterval:3];

NSLog (@ "Download second picture");
});
Dispatch_group_async (group, queue, ^{
[Nsthread sleepfortimeinterval:1];

NSLog (@ "Download the third picture");
});
Dispatch_group_async (group, queue, ^{
[Nsthread Sleepfortimeinterval:5];

NSLog (@ "Download fourth picture");
});
Notify notification, called when all asynchronous requests are complete dispatch_group_notify
Dispatch_group_notify (group, queue, ^{
NSLog (@ "Update UI");
});

4. Second way of scheduling group

Create a dispatch group
dispatch_group_t group = Dispatch_group_create ();
Get Global Queue
dispatch_queue_t queue = dispatch_get_global_queue (0, 0);

Enter queue
Dispatch_group_enter (group);
Dispatch_async (Queue, ^{

[Nsthread Sleepfortimeinterval:2];
NSLog (@ "Download first picture");

Leave the queue
Dispatch_group_leave (group);
});

Dispatch_group_enter (group);
Dispatch_async (Queue, ^{

[Nsthread sleepfortimeinterval:1];
NSLog (@ "Download er map");

Dispatch_group_leave (group);
});

Dispatch_group_enter (group);
Dispatch_async (Queue, ^{

[Nsthread Sleepfortimeinterval:3];
NSLog (@ "Download san map");

Dispatch_group_leave (group);
});

Wait for the dispatch queue wait equals a blocking state after the last asynchronous process executes, the update UI executes
Dispatch_group_wait (group, dispatch_time_forever);

NSLog (@ "Update UI");

5. Interrupt Operation

Dispatch_barrier_async must be a custom queue
The concurrent queue specified here should be created by itself through the dispatch_queue_create function. If you pass a serial queue or a global concurrency queue, this function is equivalent to the Dispatch_async function.

dispatch_queue_t queue = dispatch_queue_create ("COM.BLOGCN", dispatch_queue_concurrent); Dispatch_get_global_queue (0, 0);

Dispatch_async (Queue, ^{
[Nsthread sleepfortimeinterval:1];
NSLog (@ "1");
});

Dispatch_async (Queue, ^{
[Nsthread Sleepfortimeinterval:3];

NSLog (@ "2");
});
Dispatch_async (Queue, ^{

[Nsthread Sleepfortimeinterval:2];

NSLog (@ "3");
});

Interrupt Operation Dispatch_barrier_async
Dispatch_barrier_async (Queue, ^{
NSLog (@ "--------");
[Nsthread sleepfortimeinterval:1];

});

Dispatch_async (Queue, ^{

[Nsthread sleepfortimeinterval:1];
NSLog (@ "4");
});
Dispatch_async (Queue, ^{
NSLog (@ "5");
});

6. Traverse

Traversal operations
dispatch_queue_t queue = dispatch_get_global_queue (0, 0);
Number of iterations traversal
Dispatch_apply (5, queue, ^ (size_t i) {
NSLog (@ "%@", @ (i));
});

GCD applications and Other methods

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.