ios - Why is dispatch_sync doing work on the main thread when it uses DISPATCH_QUEUE_PRIORITY_BACKGROUND? -


working on getting concurrency going app. have snippet of code i'm curious thread exists on.

func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0))     {         // each location, update user location         location in locations         {             self.updateuserlocation(location)         }     } }  func updateuserlocation(location:cllocation) {             _controller?.centermaponuser() } 

when run this, code correctly (according think happening) gets dispatched thread.

enter image description here

what confuses me why changing dispatch call sync makes run on main thread:

dispatch_sync(dispatch_get_global_queue(dispatch_queue_priority_background, 0)) 

causes:

enter image description here

the reason confused, (think) specify background thread. understanding, making main thread wait while dispatch work background thread. isn't whats happening, main thread continues work. optimization done os? should work done on thread?

could clarify theory wrong on this?

i believe locationmanager called on main thread, dispatch_sync cause main thread blocked until block finishes executing, since main thread block , can't anything, why not use main thread execute block ? think it's optimization of gcd.

the gcd reference says:

as optimization, function invokes block on current thread when possible.

you can't assume block submit dispatch_queue execute in threads, dispatch_queue doesn't attach specific threads(except main queue).


Comments