Android Room and Firebase in a MVVM Architecture - Offline First Application - android

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.

Related

What does firebase realtime database use under the hood for offline capabilities?

I am curious to know, if firebase uses SQLite under the hood to save its data for offline use or it creates a file saving the data or anything else?
On Android the Firebase Database client uses a SQLite database for its disk based persistence. There is no documentation on this however, as it's seen as an implementation detail that you should not rely on.

Link Android Room Database with Firebase Realtime Database

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

Need to transfer the Firebase data to MySQL and synchronize

I am creating an Android project and in our app, we are going to store the abstract user data and we are creating in Firebase because of its feature of real-time updating. But for doing data analysis we need to store the data in some database, for doing the query operation we have thought of MySQL but on searching regarding my query I have found it can be done but it requires NodeJS support on the server which is not provided by our hosting provider. Is there some other way to handle this problem?

Ways to have synced offline working data in Android and iOS app

I have an android application which uses SQLite database (stored in APK) for storing offline working data. The app is used only in offline mode.
Now I want to create iOS version and I want to implement idea of synced databases on both platforms - Change database on server and it will update databases in device when will be online
My first idea was to replace SQLite to Firebase Realtime database but there is a problem with offline mode. Is there a way to cache the whole JSON for offline use?
Or is it easier to use a cloud for storing SQLite database and implement syncing with new versions of the database?
Thank you for your responses.
The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
scoreRef.setPersistedEnabled();
But this need very first internet connection to sync, this will no enable data without that first request.
For detail about the offline capabilities, head out to the official doc link here.

How does Firebase Database Instances retrieve data in realtime?

I just built my first app. It's an Group Messaging App for which I used Firebase Realtime Database. I followed this tutorial to build my app.
The chat is working flawlessly and in realtime ie any changes into the Database are retrieved and reflected within seconds on my app. Actually, being bit curious, I didn't just copy paste all those code lines instead I'm trying to understand meaning behind each statement. So, I'm confused with one of my doubts:
How does this work in realtime (chats pop up immediately)? I was reading about Firebase Database here and they mention ValueEventListener is used to update app data in realtime but what's used here?
From the documentation:
Realtime: Instead of typical HTTP requests, the Firebase Realtime Database uses data synchronization—every time data changes, any connected device receives that update within milliseconds.
Network-wise this is achieved through WebSockets, which is used both on the server and in the client side Firebase library.
Additionally, the "Realtime Database API is designed to only allow operations that can be executed quickly".
Edit: The Firebase client library sets up one WebSocket to communicate with the Realtime Database, which is used for all the communication with the Realtime Database, both reading/subscribing and updating/pushing (unless you use the REST API).
Edit 2: In the tutorial you did you used the FirebaseListAdapter which abstracts away how the data synchronization is done. It's fourth parameter is a reference to a Realtime Firebase Database location that it will sync with (using WebSocktes), and populate the list for you. It takes each entry of the synced data and puts it into new Java objects of the model class you provide as second argument, namely ChatMessage.class.

Categories

Resources