Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to always remove the listener in Firestore?

I am actually confused what the benefits of removing listener when I listen to Firestore database. I have tried to find the documentation but I can't find it

I usually always remove the listener in viewWillDisappear. from the video in here https://www.youtube.com/watch?v=rvxYRm6n_NM it is said that the benefit of removing listener is to reduce battery and data usage, based on that so I think it will be good if I always remove the listener before the view disappear.

var userListener : ListenerRegistration?

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // removing listener
        guard let userListener = userListener else {return}
        userListener.remove()

    }

but when I read the firestore pricing in here https://firebase.google.com/docs/firestore/pricing

it is said:

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

if I remove the listener, is it just the same as disconnected? because it will affect the number of document read.

so What is the best time to remove the listener or what is the advantage and disadvantage of removing listener?

like image 851
Agung Laksana Avatar asked Sep 16 '25 12:09

Agung Laksana


1 Answers

In most scenarios I see, developers remove their listeners when the view that needs their data disappears. This may indeed to additional documents having to be read on the server when you reconnect the listener, to check if they've been updated. But the alternative is to keep the connection open, which may lead to additional battery drain and bandwidth usage. Which one of these is preferable for your app, only you can tell.

like image 80
Frank van Puffelen Avatar answered Sep 18 '25 10:09

Frank van Puffelen