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
How to Check for Self with Contains() in Swift
Getting an Ip Address and Port Number from a Sockaddr_In Struct in Swift
Validate Unicode Code Point in Swift
Swift-Animate Cashapelayer Stroke Color
Swift: Draw a Semi-Sphere in Mkmapview
Automatic Select a Date in Datepicker in Swift Language
Turn Off Touch for Whole Screen, Spritekit, How
How to Bind My Array Controller to My Core Data Model
How to Hide The Top Bar (With Buttons) Usin Swift and Macos
Swift Framework with Opencv Dynamic Framework, Library Not Loaded
How to Have a Searchbar Which Shows Suggestions with Different UItableview
How to Assign Elements of a Dictionary to JSON Object in Vapor 3
Swiftui Navigationview Starting Inside Itself
How to Drag a Working Slider Using Swiftui
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent