Is it possible to force the Firestore Database to cache an entire database offline?
Currently, Firestore caches what has already been accessed in the app. I want to use data offline that the user may not have accessed before going offline.
I'm using Flutter for the app.
The only way to locally cache data from Firestore is to actually query for that data. You will need to execute queries for all the documents you wish to cache. There is currently no alternative to this.
Related
In my app, I have an SQLite/Room database. I want to introduce synchronisation between the devices of my users.
I want to combine my local SQLite/Room DB with a cloud Firebase DB.
I want my SQLite database to be stored in the Firebase Database when the users are logged in, otherwise let it store offline. Any changes to the local database should be reflected in Firebase Database when logged in. Also if a user deletes the local database he/she can retrieve it from Firebase Database. I just want to use Firebase for the synchronization between the local database and Firebase Database.
How can I do it? My app is in Android with Java as a backend.
In my app I have SQLite/Room database. I want to introduce sync between the devices of my users. I want to combine my local SQLite/Room DB with cloud Firebase DB.
I don't see any reason why you would add an additional local database to your application, since both Firestore and the Realtime Database have their own offline persistence mechanism. However, you can implement such a feature but you should take into consideration that the entire mechanism of synchronization should be managed by you.
I want my SQLite database to be stored on the Firebase Database when the users are logged in otherwise let it store offline.
This can be simply done without a local database. How can you achieve that? Simply by attaching an auth state listener to track the user auth state. This means that when you're using Firestore, you can specify the source for reading the data. For example, if the user is authenticated you can read the data from Source.SERVER, otherwise from Source.CACHE.
Also if the user deletes the local database he/she can retrieve it from Firebase Database.
There's currently no API that does that. You should implement your own mechanism. You should track the user when it deletes the database and then take some actions accordingly.
I'm creating an android app which uses Firebase Firestore database to store data. I store about 3600 questions and about 400 images. I know that firestore support offline. But I'm not sure whether the database download itself or not every time I request data from it. And under which circumstance does the firestore database get downloaded from the cloud? Does the database get only updated when the cloud database change?
Every query performed while the app is online will download all the necessary data from the database to satisfy that query.
Any data cached locally as a result of a prior query is only used when the app is offline. As suggested by the documentation, the idea is for your app to be usable when internet connectivity is interrupted. The assumption is that the connectivity will eventually come back, and queries will revert to using the online database as the primary source of data.
I have an app that has 50k - 60k document reads a day and I can't afford another plan now, so I'm looking for a way to optimize reads, getting less reads possible I can.
The database has 4 collections with some subcollections, I think I might have around 1000 documents in the whole server.
It is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache, after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, store to the cache, and keep reading from the cache?
The app could only load data from cache (using the parameter "source") and never directly from Firestore, if possible.
Firestore.instance.collection("images").getDocuments(source: Source.cache);
Is is there anyway: I can read documents from cloud Firestore (first time the user opens the app) and store on the Firestore cache
That's the default behavior. According to the official documentation regarding Firestore offline persistence:
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.
So there is nothing special that you need to do. Once you open a stream on one of your collections, the data is added to the cache.
after that, make the app only load data from Firestore cache, and if there is a new document(s), it reads these documents only, stores to the cache, and keeps reading from the cache?
That's again the default behavior, but this work as long as the documents in your database are not changed. If a document in the database is changed, you'll be charged with a document read for each document. The mechanism is the same.
Besides that, you can tell Firestore to read data only from the cache if needed, as explained in the answer from the following post:
How to cache Firebase data in Flutter?
My iOS app's firestore cache seems to have got out of sync with Firestore. As a result I've had to disable persistence. Is there anyway to reset the cache? And is there a way to make sure it's constantly in sync? All I did was delete documents from the database!
There is now a feature in the API for clearing persistence. It is not recommended for anything but tests, but you can use
firebase.firestore().clearPersistence().catch(error => {
console.error('Could not enable persistence:', error.code);
})
It must run before the Firestore database is used.
There's no API for manipulating the local cache in any way. The Firestore SDK chooses what to store based on queries that you perform.
On Android, users can manually clear the local data for an app without uninstalling it. This will remove all the data locally stored by the app.
If you have a specific use case, please feel free to file a feature request.
My iOS app's firestore cache seems to have got out of sync with Firestore. As a result I've had to disable persistence. Is there anyway to reset the cache? And is there a way to make sure it's constantly in sync? All I did was delete documents from the database!
There is now a feature in the API for clearing persistence. It is not recommended for anything but tests, but you can use
firebase.firestore().clearPersistence().catch(error => {
console.error('Could not enable persistence:', error.code);
})
It must run before the Firestore database is used.
There's no API for manipulating the local cache in any way. The Firestore SDK chooses what to store based on queries that you perform.
On Android, users can manually clear the local data for an app without uninstalling it. This will remove all the data locally stored by the app.
If you have a specific use case, please feel free to file a feature request.