Sumesh Kumar S
Sumesh Kumar S

Reputation: 15

Disable firebase sync for a particular collection unless until forced

How can I disable sync for a particular firebase collection once it is loaded? Unless it's forced it should not again check the server.

Suppose I have a user collection. i would like to read the user values from server only when the user logs in. I don't expect updated values for user collection until the user logout and sign in again.

Is there a way for it?

Upvotes: 0

Views: 466

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

If you want to force the Firestore client to read from its local cache, you can :

  1. Use get() calls with SourceOptions to force getting documents from the cache only.
  2. Call disableNetwork to disable all network access.

Note that neither of those is a great option, as Firestore is primarily a cloud-hosted database that can continue to work while you're offline for short of medium time intervals. If you want to only use Firestore to periodically synchronize data, I'd recommend using Doug's first approach and use a separate local database for you local needs.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317712

It's not possible to "disable" synchronization on a specific collection. The SDK will synchronize everything all of the time with the same behavior. If you want to load data only once, you will need to write some code to handle this situation. You could:

  1. Query against the server only once, then store the results into another local database for querying without dealing with Firestore again.
  2. Enable persistence, query against the server only once, then use the SDK's local cache for all future queries. This will not be reliable, because cached data can expire from the cache without your app knowing about it. You would need to perform another query against the server to make sure the cache is up to date.

If you must only read data once, and use that data reliably forever, you should implement the first option.

Upvotes: 1

Related Questions