A mobile inventory app must refresh its screen the moment a stock document changes in Cloud Firestore, without the client repeatedly asking the server whether anything changed. What should the developer attach to that document?
Show answer and explanation
Answer: C
A snapshot listener, created with onSnapshot in the JavaScript SDK or addSnapshotListener in the Android and iOS SDKs, keeps a connection open and delivers the current result plus every subsequent change. The client does not have to poll, and the interface reflects updates as soon as the backend commits them. This is the native Firestore mechanism for live data.
Why the other options are wrong
- A. Rewriting the document on a timer does not notify the app. The client would still have to discover the change itself, and the function adds cost and delay without providing any push channel to the device.
- B. A one-time get returns the data only at the moment it runs. Retrying it in a loop is a form of polling, which the scenario excludes, and it still leaves the screen stale between attempts.
- D. Batched writes group several mutations so they commit together atomically. They are a write-side tool and do not tell the reading client anything when a document later changes.