Why Is a Firestore Listener Returning .Added Twice When a Single Document Is Added

Why is a firestore listener returning .added twice when a single document is added?

The first added is the local event that is fired as soon as you call setData(). The second added is the acknowledged event after the server has written the data.

See the events for local changes section in the documentation.

Another reason might be that you fail to unregister your observer. If you register your observer in viewWillAppear, unregister it in viewWillDisappear or viewDidDisappear . If you don't, you'll end up with multiple observers for the same event and thus with multiple invocations.

Firebase - Firestore data query callled twice two times

The issue both the listener gets called and my data becomes doubled and the data are populated twice.

This is happening because you are using get() & addSnapshotListener() to get the data at the same time. What actually means is that you are getting the data from the database once and second, you are getting the data again but in real-time and that's why your data becomes doubled. If you want to get the data in real-time, only use addSnapshotListener(). If you want to get it only once, just use a get() call. Both methods have the same behavior but in the case of addSnapshotListener(), the listener remains active until you remove it.

Why is docChange getting called twice after delete on document?

Listeners on queries in Cloud Firestore will trigger every time the result set for the query changes. So, if you delete a document that you got from a query, your listener will trigger again to reflect that change. This is normal behavior, and there is no way around it. If you don't want to process deletions from the result set, then you will have to check the type of the document change in your listener, as described in the documentation. Your docChange has a type parameter that indicates what's different in the change than the previous snapshot.

Firestore document read costs when one new document is added in a snapshot listener

You are only charged for the document that was added. As long as the listener is added, all documents that were part of a prior result set are cached in memory and redelivered to the listener if there is a change. Even after the listener is removed, a new listener for the same query is likely to pull results from the disk cache, which also doesn't cost a read for each document.



Related Topics



Leave a reply



Submit