I am writing Kotlin Android App with Firebase Realtime Database, and I want to update GPS data in specific period(10sec), not in real time. I tried both Timer()
and Handler()
, but the update time was not accurate, sometimes less than 10sec.
I read this but it doesn't help.
val myTimer = Timer()
val task = object : TimerTask() {
override fun run() {
val database = FirebaseDatabase.getInstance()
val gps = GPS(latFirebase, longFirebase)
val mAuth = FirebaseAuth.getInstance()
val currentUser = mAuth!!.currentUser
val userID = currentUser!!.uid
val ref = database.getReference("gps_data/$userID")
ref.setValue(gps)
}
}
myTimer.schedule(task, 100, 10000)