【发布时间】:2019-03-19 20:42:40
【问题描述】:
我做的很简单。
class HomeDatasourceController: UICollectionViewController, CLLocationManagerDelegate{
let locationManager:CLLocationManager = CLLocationManager()
//CurrentLocation is my variable that isn't changing on time
var currentLocation = CLLocation(latitude: 5.0, longitude: 5.0)
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
fetchHomeFeed()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for currentLocationTemp in locations {
self.currentLocation = currentLocationTemp
print("This is latitude: \(currentLocation.coordinate.latitude) , and this is longitude: \(currentLocation.coordinate.longitude)")
}
}
fetchHomeFeed(){
print("Hello: ", self.currentLocation)
}
当调用 fetchHomeFeed 时,它只打印出我设置的虚拟位置(5.0 纬度和经度),而不是我的实际位置。 我知道这是异步的,但我不确定如何第一次至少拨打一次电话,以便我可以正确打印出我的位置(第一次)。
在此之后我还需要它继续更新,因为我想实时更新用户的位置。
我尝试在 func locationManager 中使用 fetchHomeFeed(),但这会打印出 ("Hello: ", self.currentLocation) 多次,我只希望 fetchHomeFeed() 仅被调用一次(具有正确的位置) ,然后在此之后连续调用该位置,因为我将有另一种方法使用它。
我不确定如何实现。
【问题讨论】:
标签: ios swift asynchronous cllocation