This blog has been written with an aim to provide information on how to save data in Firebase Realtime database. The data that can be written to this database can vary from simple data types, such as strings, to complex data types such as Java objects.
To use the Firebase Realtime database in Android, we need to have an Android Studio project for creating the android app and also we need a Firebase project to connect the Firebase realtime database to the Android app.
To create a new Firebase project, follow the below mentioned steps :-
1. Open the URL – https://console.firebase.google.com
2. Then click on “Add Project”.
3. Enter the Firebase Project name and accept the conditions and then proceed with clicking on “Continue” button.
4. On the next Welcome screen, click on “Add Firebase to your Android app“. Enter the package name and click on Next.
5. Download the google-services.json file to your local system, and copy this file to the location in the Android Studio project as specified.
6. Follow the small tutorial in the next step to add 3 lines of code in build.gradle files at project-level and app-level.
7. Finish adding the Android app to the Firebase project.
8. Open the Database section by clicking on the “Database” option in the left menu, and then click on “Create Database” link.
9. Now the process of creating the Firebase project is finished.
Add the following line in the app-level build.gradle file to add the library to start using the Firebase Realtime database.
Before proceeding further, let us understand what is a Firebase reference.
In the Firebase database, the data is stored at a location. The pointer to that location is known as a Firebase reference. Multiple references point to a same location if they are initiated with the same path.
Create an instance of Firebase Database by writing the following line of code:
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
Now create references of the paths where the data would be stored in the Realtime database. As the data is stored in the JSON format in the Firebase database, the paths specified in the references are the keys of the JSON object.
The following line of code has to be added to create a reference to the root node in the database:
private DatabaseReference mDatabaseReference = mDatabase.getReference();
Create a child under the root node in the database and use this child key to add the corresponding data. A string can be added in the database by following the below mentioned lines of code:
mDatabaseReference = mDatabase.getReference().child("name"); mDatabaseReference.setValue("Donald Duck");
The above lines of code would insert the data in the database. The database in the Firebase project will look like as follows:
Firebase Realtime database also supports saving complex Java objects. A model class needs to be created first for saving the Java objects. Make sure to add an empty constructor and each attributes’ getters in the class.
Following is the code for a demo model class:
Following is the code to save an object of this class to the Firebase database:
User user = new User("Mickey", 18); mDatabaseReference = mDatabase.getReference().child("user"); mDatabaseReference.setValue(user);
The above mentioned code would insert the object data in the Firebase database as follows:
Now you are having the knowledge of how to save data into the Firebase Realtime database.