Faster Way to Get/Read Data From Firestore (get vs addSnapshotListener) on Android

Apr 8, 2019
use addSnapshotListener

get

By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.

If you are online, get() will attempt to check the server for latest data (which takes between 300ms - 1500ms).

If you are offfline, get() will return result from cache (probably immediately).

You could specify Source.CACHE.

Causes Firestore to immediately return a value from the cache, ignoring the server completely (implying that the returned value may be stale with respect to the value on the server). If there is no data in the cache to satisfy the get() call, get() will return an error and get() will return an empty QuerySnapshot with no documents.

addSnapshotListener

Based on the following posts (I couldn't find any official documentation)

addSnapshotListener will return result immediately if there is a local cache, and will invoke again if the local cache differ from the server. Technically, addSnapshotListener shall return result faster than get if a local cache already exist.

NOTE: Firestore addSnapshotListener Once Only.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.