Firebase Realtime Database – Basic Reading And Writing

Firebase has become a big player in the Android development space. I really like using Firebase. It takes care of a lot of things on which you do not necessarily want to spend huge time. For example, if you’re writing a little app which needs a backend database, you do not really want to spend huge amount of time designing that database, making sure that the data transfer is 100% safe, that there is no data loss, handling what happens if the user looses connectivity etc. Firebase has got you covered. If you don’t know what a database is, check out this blog post.

Setup

I assume that you know how setup the Firebase SDK in your app, so I will skip this part. But if you don’t know how to set up the Firebase SDK (google-services.json, gradle…) than checkout this blog post.

Firstly, you’re going to need to add something to your build.gradle file:

Note, the version of the database is always changing. In order that your app is up to date, check this table of the current firebase versions. My app contains the current firebase realtime database version, but you need to make sure that you use the newest version because new features and bug updates are being added constantly.

If you’re having trouble running the gradle build script, then make sure that you have the newest version of the Google repository. To do that, simply go to Help -> Check For Updates in your Android Studio.

Template

I wrote a simple template for this example so you don’t have to waste time designing a UI. It’s a simple ConstraintLayout with a TextView, EditText and a Button.

The activity_main.xml file:

And the MainActivity.java file:

Firebase console

This example app does not feature Firebase Authentication and Firebase Realtime Database does not allow writing to the database if the user is not authenticated. We’re going to change that for this example. Note:// This is not the way you app should be set up, this is just for this simple test app.

This is a screenshot of the Firebase console. As you see, the Firebase Real Time Database, out of the box, has rules about who can read and write to the database. The default rules say that if the device doesn’t have an authenticated user, than the device can not read nor write to the database. As you may have noticed, the rules are a simple JSON file, so we’ll change these values so that every device, regardless of whether it has an authenticated user or not, can read and write to the database.

As I said, this is not the way you want to set up your Firebase project. This allows anyone with the access to your app, or your API key to write or read from the database as they please. They can change the values, or evan delete the whole database just like that. So remember, NEVER SET THE VALUES FOR READ AND WRITE TO TRUE. I plan on writing a blog post about the Firebase Realtime Database rules. If you’re interested in that, just search my blog for Firebase Database Rules, and there is a chance that there is post about it. If not than you’ll just have to wait a little bit.

Basic write to the database

Now we’ll going to add a simple value to the database. We’ll pass a test String, just to get a grips of how the Firebase Realtime Database works.

We created two variables. We need an instance of the FirebaseDatabase and a DatabaseReference. You do not need to any code to connect your app to your Firebase project. Firebase handles that for you through the google-services.json which contains stuff like your API key and things of that nature.

Now let’s look at this at detail. We created a database reference to our Firebase Realtime Database. The Firebase Realtime Database is a NoSQL database. It stores the data in a JSON file. So we created a child listView. That is a child of our database. Then we created a child called test of the listView child. Then we put a value in the test child. That value is a String Object, and it says “This is a test value”. Now let’s take a look at how Firebase stored this in our Firebase console.

As you can see, it’s a JSON tree. There is a problem tho. If we try to write to the test child of the tree again like this:

Our original value that we put in the test child is gone. It’s been overridden by the new value. You can create a new child of the test child for every database write. That method can be usable if you perhaps storing unique usernames. If you are not doing that, then you’ll need to use the push() method. The push method creates a new child with a random name. You would use it like this:

And now out JSON database tree is going to look like this:

Reading from the database

Now we’re going to take a look at how to read from the database. When using SQLite database to store data locally on your device, you need to specify exactly when to query the data, and if you’re building some kind of database where multiple devices have access to it, you would have to query the database constantly. That is not a good idea. It is both memory and power intensive. Because of that, people make the database send the needed data itself when there’s been a change to the database. You just need to write a listener for a specific part of the database. Firebase works like that.

In this bit of code, is set the string s to the value in the child test. I deleted everything from the test child that we added before and just added a single string which said “Hello”. The setTextView(String s) is a method that I’ve written that just sets the TextView text to the text in the test child.

Finish the app

Now will just add some additional functionality to our app. We’ll read the text from EditText, send it to the database, then retrieve that data and display it in the TextView.

I made a small change compared to the code we’ve written before. I set the child of the database reference right in the initialization.