Advanced usage of IOS thread GCD

Source: Internet
Author: User

A previous article on the thread of the blog has introduced the simple use of gcd and examples, today, because the project has a special application GCD examples, for everyone to introduce two special needs of the use of GCD method.


Objective: to accomplish one thing, and then do the next, to ensure the function execution cycle.

Solution: The following 2 solutions are available

1. Create barrier waiting thread

Dispatch_async (Dispatch_get_main_queue (), ^{
    
[self actionfirst];
   
});

Dispatch_barrier_async (Dispatch_get_main_queue (), ^{
   
[self actionnext];
   
});

2. Create group thread groups (that is, queue queues)


dispatch_group_t group = Dispatch_group_create ();

Dispatch_group_async (Group, Dispatch_get_main_queue (), ^{


[self actionfirst];


});

Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{


[self actionnext];


});


The code for the problem appears as follows:


-(void) Example

{
[Self actionfirst];

[Self actionnext];
}

cause : When calling the example function, because the Actionfirst function executes longer, Actionfirst may not have finished executing when the Actionnext function finishes executing, Causes a crash when executing actionnext.

Advanced usage of IOS 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.