4

I am using Firebase Realtime Database in my app. It works really well and I am quite satisfied with it. But recently I bumped into an edge case because of which some of my users are losing their data.

SCENARIO #1

The user logs into the app (Firebase Authentication) with internet connection on, and adds some data to the database.

The user signs out with internet connection on, and signs in again and the previously stored data is there as expected. The internet connection was available and the data got synced to the server.

SCENARIO #2

Now the user turns off his/her internet connection. Add some more data to the database and signs out of the app when the internet connection is off.

Now the user turns his/her internet connection back on and signs into the app in the exact same way as above.

Now the user does not see any data that was added while the app was in offline mode. I have setPersistenceEnabled(true) and it works perfectly in normal situations.

The Problem

The problem is that persistence fails when the user is in offline mode, adds some data to the database (which does not sync to the server as there is no internet) and signs out in offline mode itself and then signs in again later.

This is an edge case but some of my users have faced this and have lost their valuable data because of this bug. How to fix this? Is it the default behavior of Firebase Database? If so, is there any workaround for this?

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107

1 Answers1

1

By my app it works exactly so.

From Firebase docs https://firebase.google.com/docs/database/android/offline-capabilities

... If our app uses Firebase Authentication, the client will persist the user's authentication token across restarts. If the auth token expires while our app is offline, the client will pause our write operations until we re-authenticate, else our writes might fail due to security rules. ..

This means that if you are signed out of the application while going online, the application does not synchronize entries that you authenticate before sign out.

Only option is do not sign out by offline.

eurosecom
  • 2,932
  • 4
  • 24
  • 38
  • Yes, this is one solution. But still it is not perfect as the user was offline and have put a lot of data during that time. Now he quickly turns on the internet and signs out. He does it quick enough for Firebase to get a new auth token and sync the data to the server. The problem still happens. Is there a way to force sync all data before signing out? – Aritra Roy Nov 22 '16 at 05:22
  • I think no. If internet is offline, you can not neither to sync nor to authenticate. I have got the same problem. May be next version of Firebase to solve. Read answer at http://stackoverflow.com/questions/38281761/clear-firebase-persistence-after-logout – eurosecom Nov 22 '16 at 07:46
  • I know there is no way to sync when offline. Is there a way to force sync all data when the internet is available so that before signing out I can force sync all data and ensure internet is also available. – Aritra Roy Nov 22 '16 at 07:59
  • By answer at http://stackoverflow.com/questions/38281761/clear-firebase-persistence-after-logout The only way is to attach CompletionListeners to every write operation you do, and wait for them to complete to permit sign out. – eurosecom Nov 22 '16 at 09:28
  • What version Firebase do you use ? I am using 9.2.0. Version 9.6 - September 21, 2016 Fixed an issue that caused Android apps to cancel unsent transactions when disconnecting from Firebase Realtime Database, rather than queuing them to be sent later. https://firebase.google.com/support/release-notes/android#10.0 – eurosecom Nov 22 '16 at 09:42
  • I am using version 9.8. So that should have already been fixed. – Aritra Roy Nov 22 '16 at 10:30
  • Yes, it's same at 9.8 The only way is to attach CompletionListeners to every write operation you do, and wait for them to complete to permit sign out. And wait for next Firebase versions. Good Luck. – eurosecom Nov 22 '16 at 10:53
  • That is not a solution. There are hundreds of write operations in my app, and there is no way I can attach completion listeners to all of them. Must wait for a better solution. – Aritra Roy Nov 22 '16 at 11:43
  • I have the same problem. Did you get a solution? The firebase is very good, but the advertisement for running offiline is the "half truth" ... we can not guarantee that the user does not disconnect, it can end the battery for example. – Jonas Rotilli Dec 04 '17 at 23:39