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