This question already has an answer here:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let mostRecentLocation = locations.last else {
return
}
print(mostRecentLocation.coordinate.latitude)
print(mostRecentLocation.coordinate.longitude)
Timer.scheduledTimer(timeInterval: 60.0, target: self, selector: #selector(StartTestVC.sendDataToServer), userInfo: nil, repeats: true)
}
func sendDataToServer (latitude: Double, longitude: Double) {
SFUserManager.shared.uploadPULocation(latitude, longitude:longitude)
}
I want send data to server every 1 minute. I am using Timer.scheduledTimer and setting selector. But how could I send lat/lng params to my function?
For sending the data with
Timer
you can use theuserInfo
parameter for pass the data.Here is the sample by which you can get call of selector method and by that you can pass your location coordinate to it.
For handling that
userInfo
you need to go according to below.for your case make sure your
didUpdateLocations
is called frequently.This is exactly what the
userInfo
parameter is meant to be used for:One way to make sure your
sendDataToServer
is always uploading the latest coordinates without inputting the coordinates to the function as input arguments would be to store the values in a scope, which can be accessed by the function and use those values inside the function.Assuming you make
mostRecentLocation
a class property, you can use below code