Firebase persistence, clear Firebase cache

旧城冷巷雨未停 提交于 2021-02-05 17:55:39

问题


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;

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;

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

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

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

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.

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. Is clearing the cache before a restore a good method?
  2. If yes, how to clear the Firebase cache?
  3. If no, can you suggest me the best method to restore data.

回答1:


[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.]

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

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.

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.




回答2:


You can use the snap.metadata.fromCache flag to check if the value is coming from the cache or not. Note that if the cache value and the value from the server match it won't fire your onSnapshot twice unless you add the includeMetadataChanges flag seen below, instead it would only fire it once with the metadata.fromCache flag being set to true.

db.collection('users').doc(uid).onSnapshot({ includeMetadataChanges: true }, (snap) => {
   if (!snap.metadata.fromCache) {
        const user = snap.data();
        // Code here
    }
})


来源:https://stackoverflow.com/questions/24416541/firebase-persistence-clear-firebase-cache

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