gpt4 book ai didi

ios - 如何在调用另一个方法之前等待 didUpdateLocation 方法被调用?

转载 作者:行者123 更新时间:2023-11-28 08:22:09 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我想在用户注册时存储用户的当前位置。

因此,只要用户单击注册按钮,就会调用 locationManager.startUpdatingLocation(),然后执行注册操作。

但是,即使在 didUpdateLocations 委托(delegate)方法返回坐标之前,注册方法也会被触发,因此用户的位置在数据库中存储为 nil。

尝试在 didUpdateLocations 中调用注册方法是一团糟,因为这个委托(delegate)被调用了多次。

    func signup() { 
self.getLocation()
let point = PFGeoPoint(latitude:locationLat, longitude:locationLon)
...
newUser[PF_USER_LOCATION] = point
newUser.signUpInBackground(block: { (succeed, error) -> Void in

if let errorInSignup = error {
...
} else {
...
}
})
}

func getLocation() {
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let locValue:CLLocationCoordinate2D = manager.location!.coordinate

locationLat = locValue.latitude
locationLon = locValue.latitude
}

我只添加了部分代码。

等待接收到坐标以调用注册函数的正确程序是什么?

requestWhenInUseAuthorisation()viewDidLoad()

最佳答案

喜欢

func signup() { 
self.getLocation()

}

在这里打电话

   func locationManager(_ manager: CLLocationManager, didUpdateLocations  locations: [CLLocation]) {
manager.stoptUpdatingLocation() // initialy stop updation
let locValue:CLLocationCoordinate2D = manager.location!.coordinate

locationLat = locValue.latitude
locationLon = locValue.latitude
let point = PFGeoPoint(latitude:locationLat, longitude:locationLon)
...
newUser[PF_USER_LOCATION] = point
newUser.signUpInBackground(block: { (succeed, error) -> Void in

if let errorInSignup = error {
...
} else {
...
// call your main view
}
})
}

关于ios - 如何在调用另一个方法之前等待 didUpdateLocation 方法被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40969147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com