wait for finished asynchronous process for getting current location in iOS app -
i current location of ios device in app using cllocationmanager
, cllocation
. works fine me. delivered location params latitude
, longitude
want use self created function. problem function getting current location runs in asynchronous process , not finished while starting next function.
i use delegates in view controller getting current location.
here code in swift:
var latitude:cllocationdegrees = 0.0 var longitude:cllocationdegrees = 0.0 override func viewdidload() { super.viewdidload() //get current location , set vars latitude , logitude right values self.getcurrentlocation() //compute distance current location given location //the result of getcurrentlocation() still not exists @ time //latitude , longitude still have value 0.0 , not real value set getcurrentlocation() self.computedistancetogivenlocation(anotherlatitude: 10.0, anotherlogitude: 15.0) }
how possible wait until function getcurrentlocation()
finished before going on in code next function self.computedistancetogivenlocation()
?
i think use closures/blocks this. see many of these in cocoa frameworks names completionhandler.
so modify getcurrentlocation() method in way, takes closure/block argument. block contains self.computedistancetogivenlocation() call. @ end of getcurrentlocation() use closure/block. syntax bit tricky , haven't start swift yet. here documentation.
but i'm not sure, getcurrentlocation() does. telling location manager start updating location? should think calling computedistancetogivenlocation() directly delegate method, because way, can 100% sure location data there.
Comments
Post a Comment