Leveraging Hive Box data and Firestore Streams - firebase

On Flutter Web, I have a stream from firestore which displays the current messages in a collection but when the internet is not available I can't have access to those messages from that stream.
I Decided to use a hive box to cache the messages and return them when no internet is available.
I cache the massages using a Hive Box but it's really challenging for me to leverage providers and return the cache data first and linking that up with the stream from firestore.
Tried convert the cached data from a Hive Box into broadcast stream and combine with the firestore stream using ZipStreams and did a mapping but doesn't still work.
Please I will need help on the best way to go about this.

Firestore has its own data persistence (read/write data when offline) and it handles all the hard work so you don't need Hive.
Works on web apps, android and iOS.
See:
https://firebase.google.com/docs/firestore/manage-data/enable-offline

Related

Flutter StreamBuilder shows Firebase data even when I'm offline

This is not a problematic question, it was asked because just wanted to know what's actually going on behind it, I can't find an answer in any documentation.
When i open the app without internet it shows the data from firebase, i try to restart then it also shows, i try to clear the cache but it shows the data like it shows from the local database, Why is this happening, is there a built-in local storage or something in Firebase Flutter?
Firestore SDKs cache any data that they've recently seen, as well as any pending writes from the local client that haven't been synchronized to the server yet. On mobile clients (iOS and Android) this cache is enabled by default, while on web you can enable it with an API call.
For more on this, see the Firebase documentation on accessing Firestore data while you're offline.

Under which circumstances does Firebase Firestore database get downloaded?

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.

Force stream based on firestore snapshots to read cache

I'm trying to switch from SQLite to Cloud Firestore in my app. And ofcourse, I'm trying to reduce the number of reads from Cloud Firestore.
I get a stream from Cloud Firestore like this:
list = fireStore.collection('users')
.document('public')
.collection('info').orderBy("date")
.snapshots();
All the documents from the collection 'info' is to be read. When read for the first time, I'd like to force future reads to be from the cache. Until, let's say, 20 days has passed. Then I'd like to update the cache.
This is as I understand it, possible with DocumentReference.get() and Query.get() (click here to find out how), but as far as I have found out, not possible with my solution.
Do I need to change my approach and implement one of the methods above or is there a work around for stream as I've used it?
The snapshots method in FlutterFire is the equivalent of onSnapshot in the native SDKs. These methods listen for changes on both the local cache and the server, and cannot be configured otherwise.
The get() methods in the native SDKs and FlutterFire can be configured to get the document(s) from a specific source. So if you want to force reading from the local cache, you'll have to use get(GetOptions(source: Source.cache)).
An alternative approach could be to explicitly manage network access for the Firestore client.

AngularFIre Firebase saving data locally?

I have an app that displays a list of items. Here is what I am doing.
When the app first loads I am making an HTTP request to get the list from the firebase database.
once the list is received the list is stored locally on localStorage for future use.
On future app loads, the list is loaded from localStorage to prevent unnecessary http calls
I am doing the above programmatically, i.e, saving data to localStorage and check for new data and getting it etc.
Does firebase provide any other way to the same?
There is no built-in support for cross page-reload persistence in the JavaScript SDK for the Firebase Realtime Database. Somebody is working on such functionality in the open-source repository, but no release was made with it yet.
If you need this functionality, I highly recommend looking into using Cloud Firestore. In addition to many other benefits, it supports cross page-reload persistence.

how to enable persistance storage in firestore?

I am working on react-native with firestore and creating an offline application. The data should be stored in firestore when there is an healthy internet connection. If not, it should be stored in cache.
In firestore, there is an Enable offline data where the data can be stored offline. But, I didn't understand what and where exactly I've to write into that.
So anyone can help me out?
Thanks in advance
in your firebase file immediately after
firebase.initializeApp(firebbaseConfig)
firebase.firestore()
.enable persistence()
.catch(err => console.log(err)
The documentation for offline persistence states:
To use offline persistence, you don't need to make any changes to the
code that you use to access Cloud Firestore data. With offline
persistence enabled, the Cloud Firestore client library automatically
manages online and offline data access and synchronizes local data
when the device is back online.
So, you don't have to do anything to take this default behavior.
Firestore maintains copy of data locally, so even it writes data in offline and fetch it.
If you want to enable firestore persistence in your react native app, just copy paste the code from documentation to App.js, I am using the same and it is working fine.

Resources