Since the nuts and bolts of the Firebase Realtime Database are set up, the next stage is to explore how data can be composed or written to a database tree from an Android application. This section will give points of interest on the most proficient method to write, how to erase database tree nodes, and furthermore, outline a few strategies for taking care of database write errors.
Essentially, a reference to the database is required. Every Firebase project has its own particular devoted Realtime Database items of which can be examined by opening the project inside the Firebase console and picking the Database
option. Inside the console, panels can be selected to show data trees set away in the database, the rules outlined for fetching the access, database use estimations, and so on.
Firebase databases are usually Representational State Transfer (REST) endpoint references, which we will use to add the data. We will understand how to fetch the reference with the following code snippet:
The preceding code will fetch the reference, on the off chance that the particular path does not exist now, it is composed automatically inside the tree when data is written at that location.
Fetch an instance of your database employing getInstance()
and reference the location you need to write. You can write most of the primitive data types as they also include Java objects:
The following screenshot explains the dashboard changes after running the preceding code:

If you notice that there aren't any changes in the dashboard from the write operation, we shall attach an onFailure
callback like the following for identifying what's stopping it:
Note
Before we compile the preceding code snippet, we need to change the rules to be true
since we are no longer using any authentication service. Go to the Rules
tab and change the read and write service to be true
. When we do this, remember that the endpoint is publicly accessible by anybody who has the URL:{
"rules": {
".read": true,
".write": true
}
}
After writing the data into Firebase now it's time to read what we have written. Firebase Realtime Database syncs all the data in real time across platforms and devices. So we have an onDatachanged()
callback to read the data:
Create a model class with constructors and declare a string to fetch the database reference for a unique key to add the list of objects. The model class is as follows:
Now in the activity class using the DatabaseReference
class we can set the object value to Firebase, as follows:
The preceding code will add the object into Firebase as follows:

To read the object data from Firebase Realtime ValueEventListner()
whenever there is an update in the database in onDatachanged
callback we can read the data changes:
When the code is executed, it will result in fetching the data tree to your project. It is up to us how we make use of the data.
Since we are using unique key mechanism the data will be added under the Name
reference with a unique identifier:

In Firebase Realtime Database to listen to the data changes, we have addValueEventListener
for listening to the multiple nodes. In case you want to check the single value by adding addListenerForSingleValueEvent()
, we can do that as well:
When a listener is not required, it should be detached from the database reference object as follows:
In a simple way, DataSnapshot
can be accessed through the getValue
method. We can use the child()
method to reach to a specific path of a snapshot. Consider the following example code snippet that fetches the title:
And all the children can be accessed using the getChildren()
method. Consider the following code that is reading all the child details inside a for
each loop:
To update data, we can use the setValue()
method by passing updated values. You can likewise utilize updateChildren()
by passing the way to update data without exasperating other child nodes:
The following screenshot illustrates the updated value for the email
field:

By using the updateChildren()
method of the database reference class, we can write the HashMap data structure into Firebase Realtime Database. Let's create a HashMap and add different key-value pairs, each should be reflected in the Realtime Database:
The following screenshot illustrates the HashMap writing in the Firebase console:

Lists are compelling data structures and they help in numerous use cases. Firebase has excellent support for HashMap and lists. Users can append the data according to the unique key from Firebase, or you can create your logic to create a unique identifier. Using the push()
method a user can insert the data, and there are many ways to filter and match the data pushed. Let's see how the push()
method helps in creating a list. As usual first grab the reference to the database and then using the push()
method get the unique key. Using the push()
method we can add a new child:
Apart from allowing a database to create a list, it is also necessary to receive a data-changed notification from the list. This can be achieved through adding child event listeners. These listeners will notify the app when there is a new child added. We need to implement a couple of callbacks when we use this listener. Most commonly there is the onChildAdded()
method when a child is added, and it sends a new data snapshot with data added. Note that onChildChanged()
is called when there is an update to the existing node, and onChildRemoved()
is called when a child node is removed. However onChildMoved()
is called when any alterations change the list order:
There are many ways to perform the query on the list. Firebase has a class named Query
to access the database inside the application on specified criteria:
To delete data, you can call the removeValue()
method onto database reference. You can likewise pass null to the setValue()
method, which does the same delete operation:
The following screenshot shows the Firebase console reaction for the delete operation:

At the point when clients are disconnected from the internet, the Realtime Database SDKs employ local cache on the device to store changes. Later when the device comes online, the local data is automatically synchronized to Realtime Database. We can enable disk persistence to save the data offline from the following lines of code: