Firebase Realtime Database – Setting up

Database setup using firebase for Android

Every android application has the need to create a back end server support which we saw here, and a database support that we are going to see. There is nothing other than the Google’s own Firebase that we are considering here to create a simple but effective server/database support for our Android application. Follow the steps below to get started quickly.

 

Setting up your Firebase App

Am sure, you would have gone through the setup here, which gives you a step by step approach on how to setup your very own, free firebase app that you can use to integrate with the Android Application. The biggest advantage about firebase is, you can store upto 100,000 users and upto 10GB of user data (which is huggeeeee!!) considering that you are a freelancer or a indie developer and you are just beginning to create medium scale applications. Do not worry though, the pricing is very reasonable, if you are looking to scale your project up. The hassle of creating a new Database construct or even a server hosted to communicate with your web application is completely not there. Everything is already done for you, all you have to do is, include the SDK into your build and you are good to go.

Let’s see, how you can create your database with firebase and successfully store the data in it.

 

Step 1: Make sure, you have the Google-services.json file in your app folder

If you looked at the link, that i have put in the last section, you can see step by step procedure to setup your Firebase app and create your very own google-services.json file which is responsible for communicating with your web app hosted in firebase. Now, if you dont know what am talking about, take a look here and prepare your app for it (Its going to take you less than 5 minutes).

Once done, we are good to go. Let’s primarily start with the dependency that you have to include in your gradle.

implementation ‘com.google.firebase:firebase-database:16.0.1’

Once you have added this in your gradle and build is successful, it is time to make the connection and add records to our database.

Step 2 : Knowing about the rules for our realtime database

There are certain locks or control system to store your data in firebase. These are called “rules”. The admin has to specify these read and write rules inorder to prevent any other application from reading and writing to your database.

For our application, we will have the read and write rules as true (Since, if you have to lock them to your application, we have to create a specific SHA-1 key and bind the application to it) . I will create a separate post with locking features and how to’s for easier understanding when you prepare your app for release.

 

Realtime database in console

Enter the console and go to Develop > Database and scroll down to find the realtime database setting as above.

We have created the database in test mode as shown. It is now easy to insert delete and update from here on easily.

Step 2: Time to perform our database actions

We have created the database, and you are taken to a empty database schema. What NOW!!

This is where, it starts getting interesting. What are the 3 basic actions you perform to a database.

Yes Insert, Update and delete! These are simple to implement when you have firebase. I strongly recommend using firebase in almost all the applications as it reduces the production time. It is powerful enough to hold huge volume of data and do not worry it has got killer security features with it.

Coming on to the Actions we want to perform. Let us start with inserting a record. You know it is just one line to insert a record (Am serious it is just one line!!)

Create a databasereference instance like below

DatabaseReference mDatabase;

mDatabase = FirebaseDatabase().getInstance().getReference();

From here on, you can perform insertion by using.

 

mDatabase.child("parentnode").child(''subnodes").setValue("Your value");

or

mDatabase.child("parentnode").child("subnode").push().setValue("Your value");
We have two lines above to insert the data. What is the difference though. In a JSON setup where the data is not stored in a relational pattern rather as nodes and subnodes. The use of push() creates a unique push key to insert newer records.

If the push is not used, the value of subnode is overwritten every time we change it.

This is very very useful to store and retrieve user specific data (like user profile data or even a database about an item)

Step 3 : Knowing about listeners for our database – Reading values

Once you have successfully written to the database, it is time to know how to read the content.

This is possible through the concept of Listeners. What do we do, when there is a change in the value of the node! We wait for the value to change and read it as soon as it changes. This is same with the firebase readers. There are three major ways to read the content and they are addValueEventListener(),addListenerForSingleValueEvent(), addChildEventListener()

The primary purpose of addValueEventListener() and addChildEventListener() is to wait on the specific node for any event change (Eventvalue listener will wait for value change on that node and the childeventlistener waits for the change in value of the child of the node). The use of addListenerForSingleValueEvent() is to read the entire value only once and not wait for any change.

Usage :

ref.addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
    Post changedPost = dataSnapshot.getValue(Post.class);
    System.out.println("The updated post title is: " + changedPost.title);
  }

  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {}

  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onCancelled(DatabaseError databaseError) {}
});

You can refer here to know more about the listeners. I don’t want to create duplicate content with how the listeners work.

The major purpose of using the listeners is to create a Asynchronous callback mechanism, that is not wait for the content to load when there is a data change happening. Whenever the data change occurs, update the fields with it.

This is a very good mechanism, but there are some situations where you might need a synchronous data to be read! Say, reading a key from one table and using that key to read a user information from another table. This is a relational database approach and this can be a blocker if you are reading things asynchronously. In order to overcome this, be ready to change the way you store data.

Even though, Firebase Database is exceptionally fast, there are situations when making too much of synchronous reads in a asynchronous mechanism can slow the overall user experience. Be sure to alter the way you store content so that, it is rendered easily.

 

You can drop in comments if there are any places you are stuck at.  Check my 3 part post where you can create your own Android App under 3 hours! (Am really serious!!)

“Learn and be Curious”.

 

Leave a Comment