I use offline mode with the setting keepSynced(true)
for some RTDB nodes. It works as expected, but it would be better for me to read data from the local cache first. Currently, when read some it takes a delay of about 5-7 seconds before I get data when offline. My guess is this delay comes when Firebase SDK tries to get data from the cloud first. So can I change the default read strategy? Thanks
2
Answers
It sounds like you’re using
get()
to read the data, which is defined as ensuring you get the value from the server and so may take some time.If you want to get the value from the cache immediately, either use
addListenerForSingleValueEvent
or (preferably) useaddValueEventListener
.That only happens when you’re offline. While online, there is no way you can read the data from the cache. The Firebase Realtime Database SDK always tries to get data from the server. That’s a behavior specific to the Realtime Database, however, if you plan to use Firestore, then what you’re looking for is possible. You can call Query#get(com.google.firebase.firestore.Source) and specify the source. So in your case, it should be Source#CACHE.