i building app user clicks button , 60mins (or amount of time) keep track of them uploading location server. using 'did update locations' function send users location firebase in real-time.
func locationmanager(manager: cllocationmanager, didupdatelocations locations: [cllocation]) { }
this system works spams server sending location of user server once every second.
this data , need send users location server once every 10-30 seconds.
what can send users location once every 10-30 seconds?
class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { private var locman = cllocationmanager() private var starttime: date? //an instance variable, used previous location time. func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { guard let loc = locations.last else { return } let time = loc.timestamp guard let starttime = starttime else { self.starttime = time // saving time of first location, use compare later second location time. return //returning function, @ moment don't have second location. } let elapsed = time.timeintervalsince(starttime) // calculating time interval between first , second (previously saved) locations timestamps. if elapsed > 30 { //if time interval more 30 seconds print("upload updated location server") updateuser(location: loc) //user function uploads user location or coordinate server. starttime = time //changing our timestamp of previous location timestamp of location uploaded. } }
Comments
Post a Comment