Firebase持久性,清除Firebase缓存 [英] Firebase persistence, clear Firebase cache

查看:100
本文介绍了Firebase持久性,清除Firebase缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Firebase同步和还原数据.我使用 setValue:withCompletionBlock:方法插入,更新和删除Firebase对象.只要保存了CoreData,就会调用此方法,从而将我所有的本地更改同步到Firebase

My app uses Firebase to sync and restore data. I use the setValue:withCompletionBlock: method to insert, update and delete Firebase objects. This method is called whenever there is a CoreData save, thus syncing all my local changes to Firebase

- (void) setValue:(id)value withCompletionBlock:(void (^)(NSError* error, Firebase* ref))block;

现在,同步将所有本地数据上传到firebase,而restore将本地数据替换为firebase数据.

Now syncing uploads all the local data to firebase, while restore replaces the local data with firebase data.

- (void) observeSingleEventOfType:(FEventType)eventType withBlock:(void (^)(FDataSnapshot* snapshot))block;

我观察到 FEventTypeValue ,并使用 FDataSnapshot 从Firebase获取数据并还原本地数据.

I observe FEventTypeValue and use the FDataSnapshot to get data from the Firebase and restore the local data.

因此,在我将 persistence 设置为Firebase之前,一切对我来说都是完美的.

So everything works perfectly for me until I set persistence to Firebase.

[Firebase setOption:@"persistence" to:@YES];

现在,当启用 persistence 时,在我进行更新时,例如说将一个对象插入Firebase,然后还原,然后再还原插入之前的数据.即,新插入的对象未还原.但是,如果我再次还原,插入的对象也会还原.删除对象时也会发生同样的事情.第一次还原时,已删除的对象重新出现,而再次还原时,该对象消失.我可以看到通过Firebase数据视图正确插入和/或删除了Firebase对象.

Now when persistence is on, when I update, say insert an object into Firebase, and then restore, the data before the insertion is restored. ie the newly inserted object is not restored. However if I restore again, the inserted object is restored. The same thing happens when an object is deleted. The deleted object reappears when I restore for the first time and vanishes when I restore again. I can see that the Firebase objects are inserted and/or deleted correctly through the Firebase data view.

我不确定我在做什么错.恢复时只有问题.我认为Firebase缓存导致了此还原问题.我正在考虑在还原之前清除Firebase缓存.我的问题是

I'm not sure what I'm doing wrong here. I only have issue when I restore. I think the Firebase cache is causing this restore issue. I'm thinking of clearing the Firebase cache before I restore. My question is

  1. 还原前是否清除缓存是一种好方法?
  2. 如果是,如何清除Firebase缓存?
  3. 如果否,您能建议我恢复数据的最佳方法吗?

推荐答案

[注意:如果您可以选择使用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.

这篇关于Firebase持久性,清除Firebase缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆