Home > front end >  How to sync SQLite database with firebase?
How to sync SQLite database with firebase?

Time:07-11

My application uses the Sqlite database, but I want to offer the user to access their data on different phones if they want. I don't want to drop SQLite and instead start using firebase because firebase needs registration and authentication which for some users is not preferable. The users should have an option, whether they want to register and use firebase or stick to the offline SQLite database. If the user was offline or logged out of firebase and then made changes in the data like (add, delete), when the user logged in or backed online, I want the changes to reflect on the Firebase too. What are some steps that need to be done to accomplish this?

CodePudding user response:

I don't want to drop SQLite and instead start using firebase because firebase needs registration and authentication which for some users is not preferable.

If an authentication mechanism with one of the providers is not preferable, then you should consider implementing an anonymous authentication. What it basically means, it allows you to create an anonymous user, without having to ask for any information.

The users should have an option, whether they want to register and use firebase or stick to the offline SQLite database.

While this mechanism can be implemented, I cannot see any reason why would you do that, since both, the Realtime Database and Cloud Firestore, have their own offline persistence mechanism. The latter, for Android and Apple platforms, offline persistence is enabled by default.

If the user was offline or logged out of firebase and then made changes in the data like (add, delete), when the user logged in or backed online, I want the changes to reflect on the firebase too.

That's what the offline persistence mechanism does. While offline, all operations are added to a queue, and once the device regains connectivity, all operations are synchronized with the Firebase servers.

What are some steps that need to be done to accomplish this?

In the case of Cloud Firestore, none. In the case of the Realtime Database, simply enable it using this line:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  • Related