ViewModel + Firebase Database
Recently, I had to work in a project using ViewModel and Firebase Database and I want to share with you my thoughts about this experience.
Last changes
2017/11/09: Updated Architecture Components to 1.0.0 (stable version)
2̶0̶1̶7̶/̶1̶0̶/̶2̶0̶:̶ ̶U̶p̶d̶a̶t̶e̶d̶ ̶A̶r̶c̶h̶i̶t̶e̶c̶t̶u̶r̶e̶ ̶C̶o̶m̶p̶o̶n̶e̶n̶t̶s̶ ̶t̶o̶ ̶1̶.̶0̶.̶0̶-̶r̶c̶1̶
Firebase Database Setup
If you had used Firebase Database before, you can skip this step. If not, pay attention on the following instructions.
As you watched, Firebase Database is a realtime database with multiple clients. Well, in this article we’ll focus on Android platform.
So, the first step is configure database for Android platform:
2- Add your Android project to Firebase console
3- Add Firebase Database dependency to your app (module’s build.gradle)
compile 'com.google.firebase:firebase-database:11.2.0'
4- Configure Database rules
In this step you can set database access without authentication. Each connections to the database will be without authentication. If not, you have to set a rule to authenticate against database.
5- Write on your database
This is a piece of sample code to write in your Firebase Database from your app:
This will write in a node name “message” (it will be created if it does not exist) with the value “Hello, World!”.
6- Read from your database
You can read any data from your Firebase Database
At this point, you’ve set your Firebase Database in your Android app. Next step is create our ViewModel architecture.
Setup ViewModel
If you didn’t hear about Android ViewModel, I recommend you read this post before continue this article.
What is ViewModel? It is designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations.
First, you have to add gradle dependencies on your build.gradle:
compile "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
If you are using support library 26.1.+, you don’t need add runtime dependency. If not, you have to add it:
compile "android.arch.lifecycle:runtime:1.0.3"
It is easier and more efficient to separate out view data ownership from UI controller logic
From your Activity, you can call to your ViewModel in this way:
If the activity is re-created, it receives the same MyViewModel instance that was created by the previous activity. When the owner activity is finished, the Framework calls ViewModel’s onCleated() method so that it can clean up resources.
Create ViewModel and Firebase Database connection
This is the interesting part in this article. Up to now, we’ve set the Firebase Database by one hand and Android ViewModel by other hand. Now is the time of join them and work together.
First, we have to create our repository to manage the Firebase Database event responses.
Note we’ve added a method named “addListener” that adds the event listener to firebase database reference.
In our ViewModel, we need to reference to that repository:
And from our view (a fragment in this case) we have to add an observer to keep data updated:
And that’s it! We’ve done a bridge between Firebase Database and ViewModel architecture.
Full sample
You can review the full sample from the following repo
Feel free to comment and add any suggestions!