Reputation: 664
I have a scenario where offline data is lost, but it seems it should have synced to the server.
Scenario:
A user is logged into our Firebase application. Persistence is turned on as suggested by Firebase:
Database.database().isPersistenceEnabled = true
If the user kills the app and restarts it while offline, he/she can continue using the app to create and query the data offline. When the user regains a network connection, the data will sync. We are good to that point.
However, if the user logs out of the app while offline, he/she will need to regain a network connection to log in. At that time the app will get a new authentication token. But the data that was previously saved with the first login session/token while offline has not yet synced. Is that data lost? or is there a way to tell Firebase to synchronize that offline data using the previous or new token?
tl;dr:
How do you get firebase data that was created offline prior to logout, to sync once there is a connection?
Note: I am not using trasactions. I am aware that transactions are lost on restart of the app.
Upvotes: 2
Views: 986
Reputation: 664
After working with support at Firebase, this is what I learned. The following is true if you have security rules that require a user to be authenticated.
Data collected offline requires the authentication token in order to replicate data back to the server once a connection is obtained. The firebase.auth().signOut() command destroys the token, and so the user's data is lost.
In order to avoid losing data, you must either prevent your app from logging off, or separately store offline data on your own, and provide a way to get that data to resubmit/sync to firebase on the new authentication token.
This is an area where hopefully Firebase will improve. My thought was that Firebase should sync data based on the token used when the user was still logged in. Just because the user has logged off, he/she was logged in at the time of the data collection, even though the user was offline. I was told this would be a feature request.
Upvotes: 3