Unleash the power of Google’s Firebase Realtime Database & synchronize it with your Android app

“Once the business data have been centralised & integrated, the value of the database is greater than the value of its pre-existing parts”

In technological industries, be it for any software application, the database holds a lion’s share & is at the core of system goals & trade-offs. Database is and will always remain a vital asset and a beneficiary aspect as a whole. Considering the weightage that database holds in today’s times, it is important to have an understanding to deal with database manipulation techniques. And as a developer, it is even more important to be able to deal with it independently.

Firebase backed by Google never failed to encourage developers to have a free hand in managing the backend tasks. One of such most important & first-ever product of Firebase was Realtime Database. Firebase database does it all, be it hosting, storage or authentication. Being a cloud-hosted database it is an API that synchronizes app data and provides an essential platform for application developers to manage their app data in a well-structured pattern. In addition to managing database independently, mobile app developers have always been dependent on the backend developers for structuring the database chores.

For a developer, it’s fragmentary to only handle the front-end and not having entire control of business data structuring. In Firebase Realtime database, the actual intention of the API is to serve the developers with all the data manipulation activities and hence giving a free hand to the developer for inserting, extracting & updating the database in a customized manner. Just by adding few dependencies & a basic set-up, we can configure the real-time database in no-time. Realtime database is like having a backend without actually having a backend.

With help of some snippets, sharing with you all these simple steps with which we can integrate the Firebase realtime database in our project and can have complete control of our database.

Getting started with the integration:

Starting it up by creating a new project on Firebase console. Initially, we need to add Firebase to our Android app followed by downloading the config file and adding Firebase SDK.

Adding Firebase to our Android app

Once the project is created on the console, the next step is to add Firebase dependencies as shown in the below snippet:

Firebase dependencies

Now, going back to the Firebase console, start by creating and naming your own database.

Creating your own database

When we create our database, the security rules are private by default. In order to get access, we need to change them. We can change the rules, under the Rules tab. Here, we need to set the read & write rules to true.

Altering the security rules

After we add these dependencies, we can import and create an instance of Firebase Database and Firebase Reference. Firebase Database is basically the entry point for accessing the database which we create on Firebase. And a Firebase Reference represents a particular location in your Database and can be used for reading or writing data to that Database location.

For demo purpose, I have considered integrating real-time database for an apparel industry app. When we think about having a database for an apparel industry, a considerably huge amount of database with their respective entities comes into picture. For instance, say any clothing industry will have a set of Orders where details about the brand, quantity, size, material, time required for production, alteration, and many such aspects are considered. Here, Orders will be considered as one entity under the database. There shall be many such orders which will be differentiated by ids. Similarly, the database will collectively have many such entities. Hence, the collection of such entities will form a single database for an organization. Here, having an overall understanding of the business plays a key role as we need to add accurate data under each entity.

Uploading the data to Firebase database:

When we create an instance of Firebase Reference in our project, we are indirectly adding entity name & an array list of all the sub-entities under it. As shown in the below snippet we are creating a database entry for entity ‘Orders’

Once we are ready with the set of data we need to add inside an entity we need to update that on the backend with the help of DatabaseReference.CompletionListener() as shown below:

Data Reference Completion Listener

The ‘child’ object helps to add the list of ‘key-value’ pair of data in a subsequently detailed manner. Once we send the details, over the console it somewhat looks as below:

Types of entities in the clothing industry
List of multiple items under primary entiry differentiated by id’s

Similar to Orders, Job Card and Alter Request are different types of entities under clothing app database.

Entity wise data arrange by unique ids
Divided sub-category data

In some cases, we need to dig in more and create data for sub-data. As in here, in the case of size parameter. It is structured as Job Card -> Ids -> Size -> S/M/L/XL/XXL/XXXL. Here, we need to send a separate array list against the size parameter.

Extracting the uploading data from Firebase Database:

Extracting the data from Firebase database

In order to arrange the database in structured format at the front end, we need to extract the uploaded data conditionally. In the above snippet, onDataChange listener have been added on onStart method. This will return all the uploaded data against ‘JobCard Reference’ just like ‘Order Reference’ or ‘Alter Request Reference’. A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

Apart from this, not just a list of data is stored, but we can even upload images against each entity or sun-entity for which we use the firebase Storage dependency.

Summing it up, Firebase believes in skipping SQL and switching to JSON for database chores. Also, providing most interactive platform for performing CRUD operations. One of the major benefits of realtime database is Data syncronization lifts up wherever it was left incase when the app goes offline. And being built up on Google infrastructure Firebase is built to scale automatically.

Here concluding, Firebase database is surely a boon to developers since we can easily store & retrieve data from Firebase server without even setting up an actual server at the backend.

Android Application Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store