且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase持久性,清除Firebase缓存

更新时间:2023-12-06 09:27:22

[注意:如果您可以选择使用Cloud Firestore代替Realtime Database,则它具有更好的脱机支持.当您执行.get()时,它将自动尝试从服务器获取最新数据,并且仅当您处于脱机状态时才使用缓存的数据.您还可以专门请求从服务器或缓存中检索数据.]

[NOTE: If using Cloud Firestore instead of Realtime Database is an option for you, it has much better offline support. When you do a .get(), it will automatically attempt to fetch the latest data from the server and only use cached data if you are offline. You can also specifically request to retrieve data from either the server or cache.]

不幸的是,这是预期的行为.您可能可以通过使用observeEventOfType而不是observeSingleEventOfType

This is unfortunately expected behaviour. You can probably work around it by using observeEventOfType instead of observeSingleEventOfType

基本上,每当您观察到数据时,我们都会首先从持久性缓存中提取数据.该数据将是我们上一次从Firebase接收到的数据.因为您使用的是而不是observeEventOfType的observeSingleEventOfType,所以您不会从Firebase接收定期更新,因此我们缓存的数据实际上不会包括您编写的最新数据.

Basically, whenever you observe data, we're going to pull data from our persistent cache first. That data will be whatever data we last received from Firebase. Because you're using observeSingleEventOfType instead of observeEventOfType, you're not going to be receiving regular updates from Firebase and so the data we have cached actually won't include the latest data that you wrote.

作为一个简单的解决方法,您可以仅在有问题的数据上添加一个observeEventOfType.您实际上不需要对事件做任何事情.但是,如果您一直听着他们的话,那么您的应用将从Firebase获取最新数据,并且当您调用observeSingleEventOfType时,您可以确信它将缓存最新数据.

As a simple fix, you may be able to just add an observeEventOfType on the data in question. You don't actually need to do anything with the events. But if you listen to them all the time, then your app will be getting the latest data from firebase and when you call observeSingleEventOfType, you can be confident that it'll have the latest data cached.