Not retrieving new data after setting Firebase Persistence Enabled to true?

烈酒焚心 提交于 2019-12-23 03:48:09

问题


After making FirebaseDatabase.getInstance().setPersistenceEnabled(true); When query to get data

Query  query = FirebaseDatabase.getInstance()
                      .getReference()
                      .child("notifications")
                      .child("entities")
                      .child(FirebaseAuth.getInstance().getCurrentUser().getUid().toString())
                      .orderByChild("seen")
                      .equalTo(0); 

From data tree

it gives old data that is in cache instead of new changed data in Firebase databse.But When I change setPersistenceEnabled(true) to setPersistenceEnabled(false) then everything is fine While I also want firebase data to be persisted in cache and also change When change is made to Firebase database.Please help to maintain Firebase data persistence along with realtime change.


回答1:


If you are using:

setPersistenceEnabled(true);

It means that you'll be able to query your the database even if you are offline. This is happening because Firebase creates a local copy of your database that will persist after your app/device restarts. Every change that is made while you are offline, will be updated on Firebase servers once you are back online. To be more clear, every client that is using a Firebase database and uses setPersistenceEnabled(true) maintains it's own internal (local) version of the database. When data is updated, it is first written to this local version of the database.

So, by enabling persistence, any data that the Firebase Realtime database client would sync while online, persists to disk and is available offline, even when the user or operating system restarts the app. This means that your app will work as it would be online by using the local data stored in the cache.

But, there is no way to stop the retrieval of the data from the cache while you are not connected to the server, as you cannot force the retrieval of the data from the cache while you're connected to the server and unfortunately this behaviour cannot be changed.



来源:https://stackoverflow.com/questions/50149787/not-retrieving-new-data-after-setting-firebase-persistence-enabled-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!