ios - What's the work around to avoid view blocking using dispatch_async() -



ios - What's the work around to avoid view blocking using dispatch_async() -

i have view couple of labels , map. in view i'm displaying info geo-location point:

the address the map + pin a first label (calculated) a sec label (calculated)

once view loading need calculation update labels. calculation taking few seconds (i need phone call api) i've set them in queue:

dispatch_async(dispatch_get_main_queue(), { allow loc = cllocationcoordinate2d(latitude: self.place!.location.latitude, longitude: self.place!.location.longitude) allow tmp = int(geo.apicall(self.currentposition, coordto: loc)); self.label1.text = " = \(tmp) unit"; })

i've used main thread dispatch_get_main_queue() because need update label. problem it's blocking rendering of map , it's blocking other async function clgeocoder gathering address geo-location point.

let loc = cllocation(latitude: self.place!.location.latitude, longitude: self.place!.location.longitude) clgeocoder().reversegeocodelocation(loc, completionhandler: {(placemarks, error) in if error != nil { println("reverse geodcode fail: \(error.localizeddescription)") } allow pms = placemarks [clplacemark] if pms.count > 0 { allow pm = placemarks[0] clplacemark self.address.text = abcreatestringwithaddressdictionary(pm.addressdictionary, false) } })

so view displayed correctly, label updated @ same time, few seconds after view displayed, , map loading when label rendered.

what best work around avoid blocking when view rendering?

dispatch_async(dispatch_get_global_queue( dispatch_queue_priority_default) { //background thread allow loc = cllocationcoordinate2d(latitude: self.place!.location.latitude, longitude: self.place!.location.longitude) allow tmp = int(geo.apicall(self.currentposition, coordto: loc)); dispatch_async(dispatch_get_main_queue()) { //run ui updates self.label1.text = " = \(tmp) unit"; } }

get background thread work doesn't straight involve changing ui, , phone call main queue when needed. credit to: understanding dispatch_async

edit: alternative utilize afnetworking (for objective-c) alamofire (for swift), asynchronously phone call api, , can alter label in completion handler: https://github.com/afnetworking/afnetworking , tutorial: http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

edit-2: wasn't paying attending , mixed objective c calls swift declarations :p

ios multithreading swift

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -