0

In my android app, I'm using firebase database. For offline work, the following code is used :

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Data in a reference will remain same. While offline, data in that reference loads without any delay, but while online the same data takes time(around 8 seconds) to get loaded. As there is no change in data in that reference, I want those data to be loaded without delay (may be from cache). How can I do this?

JNK
  • 3
  • 2
  • This is not enough information. Where is the time spent? How many and which database queries are executed? – Laurenz Albe Dec 02 '18 at 11:59
  • @LaurenzAlbe, In a reference, there are 8 nodes. I'm displaying the key of the nodes in a recyclerview in this way : `reference.addValueEventListener();` While offline, the keys are displayed smoothly, but while online, it takes time to get loaded. – JNK Dec 02 '18 at 12:06
  • So you want to get data only from cache? Please responde with @AlexMamo – Alex Mamo Dec 03 '18 at 10:32
  • @AlexMamo Yes, from cache, both while online & offline. My problem is, while online, it's taking some time to load data though there is no change in that database reference. While offline, data is loaded almost instantly. – JNK Dec 03 '18 at 14:14
  • So if you want to get data only from the cache, how to get data from online? – Alex Mamo Dec 03 '18 at 14:21
  • @AlexMamo What I want is it'll load data from cache, but if there is any update in the database reference, only then it'll load data from online – JNK Dec 03 '18 at 14:30
  • @AlexMamo See, my actual problem is - It's taking some times to load data when it is online, but loads almost instantly while offline. I want to load data instantly whether it is online or offline. Is it possible? There will hardly any change of data in that database reference. – JNK Dec 03 '18 at 14:34

1 Answers1

0

What I want is it'll load data from cache, but if there is any update in the database reference, only then it'll load data from online

So this is what Firebase does. The whole idea is that you are synchronized with Firebase servers and once new data is added your onDataChange() is triggered with a DataSnapshot object that contains the new data. If you don't want this feature you can explicitly tell Firebase to goOffline():

Manually disconnect the Firebase Database client from the server and disable automatic reconnection.

So you can use this feature, if this is what you need it.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • So, why it is taking time to load the data when there is no change in the database? – JNK Dec 03 '18 at 14:43
  • I want to load the data instantly just like it does while offline. – JNK Dec 03 '18 at 14:44
  • I understand. In this case, please see my answer from this **[post](https://stackoverflow.com/questions/48863393/firebase-listeners-first-time-invocation-takes-time)**. But don't use Firebase as an offline database. It is really designed as an online database that came work for short to intermediate periods of being disconnected. If you use goOffline(), the end result of this will be that the data on the server stays unmodified. Then what is the purpose of a Realtime database? So I suggest use this database for its online capabilities. – Alex Mamo Dec 03 '18 at 14:54
  • So, while online, it won't load data from cache but from server, right? – JNK Dec 03 '18 at 15:29
  • To reduce the delay time, i used ref.AddValueEventListener() in my main activity though it's actually needed in another activity. Then I noticed it loads data faster. Is it good practice? – JNK Dec 03 '18 at 15:33
  • It will load data from the cache, if the cache is available. If new data appears, it gets the data from the server. Nothing is triggered if no new data appears. Yes, it is. But in this case, don't forget to remove the [listener according](https://stackoverflow.com/questions/48861350/should-i-actually-remove-the-valueeventlistener/48862873) to the life-cycle of your activity. – Alex Mamo Dec 03 '18 at 15:36