Firebase persistence, clear Firebase cache

Thahir picture Thahir · Jun 25, 2014 · Viewed 12.1k times · Source

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.

Answer

Michael Lehenbauer picture Michael Lehenbauer · Jul 1, 2014

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