I am using Android Room Database for creating the database for my android app. It works perfectly for me locally but I cannot link to a server to have it online as well.
I am using Firebase for Authentication. I am trying to use Firebase Realtime Database to save the whole database object from Room and load the correct database on app startup according to the authenticated user.
I want to ask if this is possible at all? and if I can just save a whole instance of Room database or I need to re-create the database on Firebase and save my data item by item?
I also can't seem to be able to get access to the database data of Room, as when I get an object of the AppDatabase class it doesn't really pass the data. And I don't know how should do the opposite, to assign the data retrieved from Firebase later to the local data saved?
Also if it's not possible with Firebase, do you have any recommendation for some other server I can use with Room?
After a lot of researches and looking desperately for an answer here's what I reached:
Firebase already got a straight forward way to create the database and host it online. (I had my database already created so was trying to save time, but creating it from scratch using Firebase Realtime Database was a lot faster)
Room Database is quite perfect if you are planning to save your database locally and offline (Up to the post date)
I think their is no need to connect your Room Database with Firebase Realtime database because,okay first try to solve this problem, why we use Room Database ?because when we don't have internet, we can get data from Room Database , no need of network... Firebase also provides offline mode , it can also save data in persistence, so why we use Room Database ?.. Hope you got the point...
Similar project created by me which uses
Firebase authentication to login user
Save and cache user notes to sql lite database with Room
Save user notes to firebase base database
✍️ Simple Note Making App use Sqllite Room 🧰 for caching the notes and 📥 Firebase Database for online storage
https://github.com/LanguageXX/Simple-Note-App-with-Online-Storage
Hope that helps you
Related
Can Android Room Database be used as an (offline/local data source) and Firebase Database as the (online data source) in an MVVM repository (gluing the data together) refer to the figure below. Having the same database table and column setup. The application can be used and make changes offline and later sync the modified version on firebase when the internet is available.
Figure 1: MVVM ROOM FIREBASE FIGURE
Figure 2: Data Layer on Repository
Problem: I already have a room database following MVVM architecture but can't figure out how to integrate firebase as my online data source and synch to tables.
Knowing: Android's Room database is a SQL database, while Firebase's Realtime Database is a NoSQL database.
If this is possible, how firebase can be set on as an online data source connected to the repository in a simplified version or an article I can follow? Or is there an alternative wherein, a database is suggested instead?
Can Android Room Database be used as an (offline/local data source) and Firebase Database as the (online data source) in an MVVM repository?
For sure it can be, but I cannot see any reason why you would do that. Why? Because the Realtime Database has its own offline persistence mechanism. This means that your application will continue to work even if the user device temporarily loses its network connection. Besides that, all the cached data is available while the device is offline and Firebase resends any writes when the internet connectivity is restored.
The application can be used and make changes offline and later sync the modified version on firebase when the internet is available.
You're describing exactly how the Realtime Database works if you enable disk persistence using the following line of code.
FirebaseDatabase.instance.setPersistenceEnabled(true);
If this is possible, how firebase can be set on as an online data source connected to the repository in a simplified version or an article I can follow? Or is there an alternative wherein, a database is suggested instead?
I don't see why would you add an extra level of complexity when the Realtime Database already handles that for you. However, if you still want to do that, then you have to check inside the ViewModel class if the device is connected to the internet or not. And according to the connection state, you should request the data either from the Realtime Database or from Room.
Here are some useful links:
How to read data from Room using Kotlin Flow in Jetpack Compose?
How to read data from Firebase Realtime Database using get()?
If you consider at some point in time try using Cloud Firestore, then I also recommend you read the following resource:
How to read data from Cloud Firestore using get()?
Remember, that in Cloud Firestore, for Android and Apple platforms, offline persistence is enabled by default.
I have been working on an Android application that populates a Google Map with markers from a Room Database and all user data is stored in a Firebase Realtime Database.
The application first copies the Room Database from the assets folder, so all the changes will take place in the copy of the database. The Room Database has 11 columns at the moment and only one of the column's values are changed.
Now I have reached the part where I would need to save the copy of the Room database to the corresponding user in the Firebase Realtime Database, so that if the application is uninstalled or if anything else happens, then the data and changes related to the markers would persist.
How could I save the copy of the Room Database in the Firebase Realtime Database and how can I pull that copy from the Realtime Database and set it as the version to use?
I did come across people looking for a solution to a similar problem but the solution so far has been to migrate the Room Database to the Firebase Realtime Database.
example: Link Android Room Database with Firebase Realtime Database
I also thought about getting rid of the Room Database and just using the Firebase Realtime Database but the management of the data related to the markers seems difficult not to mention the amount of reprogramming. Also if this is the best/only way to go, then if I have the database stored on Firebase, should each user have their own copy of the database, or should it include only the data that changes and a reference to the corresponding item in the main database with the rest of the data?
I just recently started using Firebase and I am not sure what would be the correct approach.
Thanks in advance!
Android's Room database is a SQL database, while Firebase's Realtime Database is a NoSQL database. There is no direct mapping from the data in your SQL database, to Firebase's NoSQL model.
But it's not clear why you'd want to store the data in Firebase. If you just want a place to store the Room files so you can retrieve them later, you might be better off using Cloud Storage for which Firebase also has SDKs. You can just upload the SQLite file to Storage, and retrieve it from there later.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
How to change behavior of firebase querying for server database or locale database.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(false);
Is that possible to make same query for server and than used it from local database. firstly I want to make a query from server database and then without download again and again I want to use same querying datas from local datas even after app is closed.
I am using above codes for using offline firebase and trying to use same datas without download again
When you are using the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
It means that Firebase will create a local copy of the database on user device. This means that the application will work even if the device temporarily loses its network connection or if the user restarts the application.
So the answer your question is, that the same query that works when you are online will also work when you are offile, because the database which will be queried is the local database. You don't to do other operations.
Recently I stored data on firebase by converting excel file data into .json and then importing it into firebase database. I don't know how I should proceed with below problem-
How to store data in Sqlite such that it checks firebase only if new data is available. For example- when new user opens the app, it fetches the data and stores all data in sqlite database. When he opens app again, it checks in splash screen if same data is available on firebase, then it won't store in sqlite. If some new data available, append to Sqlite. How should I proceed with this? If any other best way also commment.
If my approach is right. Can you guide me in using the approach. I m new.
Firebase can persist the data itself locally.
You can enable disk persistence with just one line of code.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Do you really need to store the data in another sqlite database?
I'm using Firebase data persistence in my app. When I logout of the app, I want to clear the Firebase cached data as well. The persisted data is hampering the behavior of my app.
From this question, I understand that it needs to be worked around. If Firebase stored data in SQLite, how can I delete this SQLite file programmatically?