I tried looking this up but could not find a way to stop the execution of a firebase query of type observe
. Observe takes a call back and keeps running it every time the database is updated at a reference. But I want to stop this callback from running when the view disappears and again start running the query when view will appear.
So this is what I want to do. Unfortunately the observe functions returns an int and not any handler that can be used to stop the query's excution.
func viewWillDissappear(animated: Bool) {
...
stopQuery()
}
func viewWillAppear(animated: Bool) {
...
startQuery()
}
func startQuery() {
Database.database().reference().observe(.value) { (snapshot) in
//Rest of callback to be executed only when view will appear and is visible
}
}
func stopQuery() {
//Stop query callback execution started by startQuery
}
Note: I don't want answers that rely on using a vairble to check if view is visible and skip the callback execution. I want to compeletly cancel the currently running query and restart it the next time view will appear.