How to sync SQLite database with firebase? - java

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?

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

Android: Using Room and Firestore?

I wrote an app that saves and uses data in/from a Cloud Firestore database. I would want to organize more clearly than for the moment my files, for example by using DOs and DAOs.
I know the concept of "DAO" exists in Android Room.
However, the documentation seems to define Android Room as a "local database". So if I understand it well, I shouldn't use it in addition to Firestore?
By the way, it would be the same with Firebase Cloud Realtime Database (a third database system).
Edit :
I didn't understand the notion of "local" database (Room). Tamir, in his answer, corrected me. This question is off-topic.
So if I understand it well, I shouldn't use it in addition to Firestore?
No and this is beacause Cloud Firestore has offline persistence enabled by default:
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
This means that you'll have by default a local copy of your database. So there is no need to add another one.
Basically, when you are developing an app there would be some data that you will want to save into locally and other data that you will want to save on remote database, it's not a bad thing to have both remote and local database.
Some example that I can think of for saving your data remotely is to manage users - when a new user will be created you will want to check if the username is not taken, and you can't do it if this data is only stored locally.
And for using a local database - one of the major advantages in the local database vs remote database its the speed of writing and receiving data.
Here is a nice article on the subject.

Data storage in Firestore For Offline App

I am using Firestore as a NoSQL databse in my Android app. I want to store data locally in my Android device and avoid syncing with Firestore server in the initial stages of app. Is is possible for the data to persist on device even if app or mobile device restarts without syncing with Firestore database? If yes then how do I do it ?
I want to store data locally in my android device and avoid syncing with Firestore server in the initial stages of app.
According to the official documentation regarding Firestore offline persistens:
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
And to answer the second question:
Is is possible for the data to persist on device even if app or mobile device restarts without syncing with Firestore database?
Yes it is. This kind of persistens is called disk persistence, which means that recently listened data (as well as any pending writes from the app to the database) are persisted to disk. The data in this cache survives app restarts, and phone reboots.

Firebase limited service account

I have a java client (Standalone app) that is using the Firebase Admin SDK, because I need to read values from the Realtime database whenever a value is changed. (A ValueChange listener is being used).
Currently I'm including the service-account.json in the app. Even if I set the roles to Viewer, using this service-account.json, I can create accounts usingFirebaseAuth.createCustomTokenAsync, which is something I dont want.
Is there a way to make a service-account with Realtime-database read only?
I know I can use setDatabaseAuthVariableOverride to "limit", but if someone extracts my service-account.json from the app/jar, they have the power to do everything..
I'm not using node.js, just Java with spring. And js firebase is a no, because I need to receive updates even without a web page.
Never distribute service accounts to end users. They should only go to trusted parties.
There is currently no fine-grained way to control access to Realtime Database via service accounts. Access control is performed via Firebase Authentication client libraries, which are not available for non-Android Java clients.

Using sqlite database

I am working on an application where users go on and offline. I want the data entered by users to be synced with central db.
The application is in Swing and using web services to update data in central DB.
I am thinking SQLite as a solution but I am concerned about security. The concern in each client system will have more than one user and every user will have his specific data.
Does SQLite support this?
Are there any other alternatives for SQLite in this scenario?
I have Oracle 10g as my Central DB. The role of SQLite is a local DB that stores data till user goes online
EDIT:: I am here concerned about the security of SQLite file. Through my initial analysis I found there are no features like authentication while accessing sqlite file.
Hope I am clear now.
SQLite does not support per-user authentication (you can use a password, but it's the same for the whole database file). Per-user authentication wouldn't be efficient anyway, because a user who has read-access to the database file could just read the file directly. You could encrypt the data, but you would have to store the decryption key and algorithm in the executable, where the user can access it.
When you want to protect the local data of each user, I would thus recommend to delegate this responsibility to the operating system. Just store an individual database file for each user in the private user directory of the current user (%appdata% on windows, ~/ on unix). As long as users do not have admin/root rights, the operating system should prevent them from accessing data in each others user directories.
This, of course, only works when each user of your application also has an individual account on the operating system. When that's not the case, you could still use individual databases for each user stored in a public location, but encrypt each database file with a key which is derived from the password of each user.

Registration in app right way

I am want to do registration in my app. So i have made all requests to my web service.
I now when user is registered.
Now i need to save somewhere that he is registered that don't give him registration form again.
How to do this ?
Do i need to save somewhere in my sqlite database and every time when app lunches check if he is registered user. Or maybe in app settings i could save this information.
I am new to developing android app and i am searching for the right way.
Thanks all for help.
You could save that with SharedPreferences. Check the link for sample code.
http://developer.android.com/guide/topics/data/data-storage.html#pref.
This data will be removed if the users uninstalls the app or clears all data from system menu.
Edit: If you really want to remember preferences across installs you should try Backup API. http://developer.android.com/guide/topics/data/backup.html
The best solution is to use Android's Account Manager to handle user account. This aproach is the most secure (credentials are not exposed), however it has two drawbacks:
It's a little bit complocated to set up
It requires at least API version 5
Once the user registered you can save in your sqlite database and you have to check for each time when your application launches. But if the user uninstalled the application and tried to reinstall it ll ask for registration again.. In order to avoid this you have to store the information like the device id and the registered details in some servers and also in sqlite database and have to check when the application launches...

Categories