CLLocationManager
및 CLLocation
을 사용하여 내 앱에서 iOS 기기의 현재 위치를 가져옵니다. 그게 잘 작동 해. 제공된 위치 매개 변수 latitude
및 longitude
다른 자체 생성 함수에 사용하고 싶습니다. 그러나 문제는 현재 위치를 가져 오는 내 함수가 비동기 프로세스에서 실행되고 다음 함수를 시작하는 동안 완료되지 않는다는 것입니다.iOS 앱에서 현재 위치를 가져 오는 비동기 프로세스 완료 대기
현재 위치를 가져 오는보기 컨트롤러에서 대리인을 사용합니다.
var latitude:CLLocationDegrees = 0.0
var longitude:CLLocationDegrees = 0.0
override func viewDidLoad() {
super.viewDidLoad()
//get the current location and set vars latitude and logitude with right values
self.getCurrentLocation()
//compute distance from current location to given location
//the result of getCurrentLocation() still not exists at this time
//latitude and longitude still have the value 0.0 and not the real value set by getCurrentLocation()
self.computeDistanceToGivenLocation(anotherLatitude: 10.0, anotherLogitude: 15.0)
}
어떻게 내 기능 getCurrentLocation()
다음 기능 self.computeDistanceToGivenLocation()
와 코드에 가기 전에 완료 될 때까지 기다릴 수 있습니다 : 여기
는 신속 내 코드?
그렇게하지 마십시오. 전체 UI를 차단합니다. 위치 관리자 대리자 메소드에서 'computeDistanceToGivenLocation ...'을 호출해야합니다. 즉 일단 위치 정보가 있으면 Delegate 메소드의 UI 업데이트를 기본 대기열에 디스패치해야 함 – Paulw11