When using Cloud Firestore, offline persistence:
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
Which means that Firestore will create a local (internal) copy of your entire database on users device. So the same thing that you want to get can be also achieved without adding an extra local database because by default is one that already exists.
Furthermore, if you need to get the data from the cache only, you can achieve this with the help of the DocumentReference.get(Source source) and Query.get(Source source) methods.
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.
So we can now pass as an argument to the DocumentReference
or to the Query
the source so we can force the retrieval of data from the server only
, chache only
or attempt server and fall back to the cache.