IOS GCD Dispatch Use note

Source: Internet
Author: User
Tags gcd

classViewcontroller:uiviewcontroller {vardbqueue:dispatch_queue_t =0    Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib.Dbqueue= Dispatch_queue_create ("com. Innocellence.sunflower", nil)//Do some-dbqueue from main thread       Dispatch_sync(Dbqueue, {(), Voidinch              //Do some work on main thread from DbqueueDispatch_sync (Dispatch_get_main_queue (), {(), Voidinchself.test ()}) }) println ("This was some other code")} func Test () {println ("This is test")            }}

The above code, is wrong, viewdidload can not run out

and replace the code a little bit.

Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib.Dbqueue= Dispatch_queue_create ("com. Innocellence.sunflower", nil)//Do some-dbqueue from main thread        Dispatch_async(Dbqueue, {(), Voidinch              //Do some work on main thread from DbqueueDispatch_sync (Dispatch_get_main_queue (), {(), Voidinchself.test ()}) }) println ("This was some other code")    }

This one has an output, and the result is

This was some other code

This is test

and turn it down.

 Overridefunc viewdidload () {super.viewdidload ()//additional setup after loading the view, typically from a nib.Dbqueue= Dispatch_queue_create ("com. Innocellence.sunflower", nil)//Do some-dbqueue from main threadDispatch_sync (Dbqueue, {()-Voidinch              //Do some work on main thread from Dbqueue           Dispatch_async(Dispatch_get_main_queue (), {(), Voidinchself.test ()}) }) println ("This was some other code")    }

The output is as follows

This was some other code

This is test

Why is the first one unable to continue execution, first the code blocks the execution of the main thread, requires that it be returned after the new threads have been executed, but the new thread is blocked, requiring the code to return after the main thread is finished, thus forming a loop. This thread is not used in the word, in fact tightly is a thread in the execution of a sequence of code. We need to study the reason carefully!

IOS GCD Dispatch Use note

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.