On my main CollectionView
(the app home page), I would like to update the locations (i.e. the cells themselves) based on the closest location to the user. I am storing my locations in Firebase
and will be using GeoFire
and core location in order to properly list out the locations in distance order from the user.
Other posts have said that the best practice for observers involves loading it in viewWillAppear;
however, I have been unable to obtain the user's location before this runs and do not have a centered point for my GeoFire
query. Most examples related to CoreLocation
involve maps or preselected coordinates, but I wasn't planning on using a map within this controller and want to dynamically load based off of user location.
I have researched loading didUpdateLocations
within AppDelegate
, but have not been able to call that method there either. What is the best way to get user location coordinates before running a GeoFire
query when loading that returned data in viewWillAppear
or do you need to set a region and check if a user has entered within that region?
var locationDict = [CLLocation]() //using this variable to pass values outside the function to the query
override func viewWillAppear(animated: Bool) {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let recentLocation = locations[0]
locationDict.append(recentLocation)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}