Chapters

Hide chapters

Saving Data on Android

Second Edition · Android 11 · Kotlin 1.5 · Android Studio 4.2

Using Firebase

Section 3: 11 chapters
Show chapters Hide chapters

14. Reading to & Writing from Realtime Database
Written by Fuad Kamal

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last chapter, you integrated Realtime Database into your app. You added Firebase SDK to your app and connected the app with the Firebase project. You also learned about database rules and you set them up to allow only authenticated users access to the database. You even wrote your first data to the database which was just a sneak peek of what you’ll do in this chapter.

This chapter will teach you how to work with Realtime Database data. You’ll learn how to read and write data as well as how to do basic manipulation with that data. First, you’ll learn about performing CRUD operations on to the Realtime Database. CRUD is just an acronym for the four basic types of SQL commands: Create, Read, Update and Delete. You’ll combine all these concepts together in order to build a fully functional app with Realtime Database as the backend.

Setting up Firebase

You need to set up Firebase in order to follow along. Do the following steps:

  1. Create a project in the Firebase console.
  2. Enable Google sign-in.
  3. Set security rules to the test mode to allow everyone read and write access.
  4. Add google-service.json to both starter and final projects.

To see how to do this, go back to Chapter 12: “Firebase Overview” and Chapter 13: “Introduction to Firebase Realtime Database.”

Reading and writing data

Open the starter project, and build and run your app. If this is the first time you’re running this app you’ll need to sign in first in order to use it. To sign in, tap Sign in with Google and follow the steps on the screen. Next, click on the floating action button. A new screen opens where you can write your post. Write something and click POST.

Egfuwx a Lowq.
Afpuzt i Temx.

Saving data to the database

Open RealtimeDatabaseManager.kt. Add the following line to the class:

Walotudi Pograpa jgujauj - Wusy aq ziveh ot nyo wuwaqeve.
Ruropilo Vudsexa dduroaz - Gexm at zihoq eq nyo basokore.

Fetching data from the database

When it comes to reading the data from the database you have two options. You can read the data once or you can be notified whenever data changes. Since you want to see every new post from other users instantly, you’ll implement the second option.

Tpe udib xuj klilouc mqi nizm oczam ge kyi wolamuyi.
Qfu umep ren bkuseir thu hiwl acnof li kja yidojire.

Updating and deleting data

Tap on a post to open another screen that shows post details.

Rbu Ganl Sojoavm Sjfoul.
Wni Wivc Ruliixj Cfbool.

Updating

Updating data in Realtime Database is almost the same as writing. You use the same method for updating - setValue() . Above the RealtimeDatabaseManager class declaration, add next constant:

Deleting

Deleting data in Realtime Database is very simple. You have two options. You can delete data by using setValue() and specify null as an argument or you can use removeValue() which will set the value at the specified location to null. You’ll use the latter approach.

Querying and filtering data

To show how to query data you’ll add another feature to the app. You’ll enable users to add comments to the post.

O juvvunv ugcof cefhaxctojzm.
U neccogz ozloy nolrunqqiwxq.

Rwe Kuxforl ak qukuwyi up rqo Tokufeca larceqi.
Vbo Rohqulk uj ralertu ih tfi Zecadova loqwele.

Fro pihtibp im ketarxa ef qca Jusz Dekaimz Lqqooc.
Zga nichanr om citehle ig hdo Pagw Qakiujj Zbjiad.

Other features

Transactions

Realtime Database also allows you to write data to the database using a transaction. A database transaction is a unit of work that is independently executed and it must be atomic, consistent, isolated and durable. If the WhatsUp app had a feature to allow you to “like” a post you could use transactions to keep track of how many likes a given post had. Since there is a use case where multiple users could “like” the post at the same time, the transaction would allow you to always have fresh and correct data about likes.

Listening for child events

Previously you saw how to use value event listener. Often you’ll also need to know about changes in children of a specific node. In that case, you’ll need to use a child event listener. Child event listeners notify the app when child nodes are added, deleted or moved within a parent node. To add a child event listener you’ll need to call addChildEventListener() on a database reference instance, and there are four methods that you’ll need to implement. Check the official documentation https://firebase.google.com/docs/database/android/lists-of-data#child-events to learn more about child events.

Indexing

There can be a performance issue if your app frequently queries the database. To improve query performance you should consider defining indexing rules. A database index is a data structure that is used to quickly locate and access the data in a database.

Key points

  • The FirebaseDatabase object is the main entry point to the database
  • DatabaseReference represents a particular location in the database and it is used to refer to the location in the database to which you want to write to or read from.
  • push() is used to create an empty node with an auto-generated key.
  • Firebase Realtime Database has several types of listeners, and each listener type has a different kind of callback.
  • ValueEventListener listens for data changes to a specific database reference.
  • ChildEventListener listens for changes to the children of a specific database reference.
  • You need to decide how to handle listeners when the user is not actively interacting with the app. In most cases, you want to stop listening for updates. To do that you need to remove the listener.
  • For updating data in Realtime Database, use setValue() .
  • You can delete data by using setValue() and specify null as an argument or you can use removeValue() which will set the value at the specified location to null.
  • A query is a request for data or information from a database. The Query class is used for reading data and it has many useful methods that allow you to fetch the data in a way you want.
  • A database transaction is a unit of work that is independently executed and it must be atomic, consistent, isolated and durable.
  • To improve query performance you should consider defining indexing rules.

Where to go from here?

You covered a lot in this chapter. You have seen how to write data to the Realtime Database, how to listen for changes in the database and how to update and delete data. It takes a little bit of practice to get used to working with Realtime Database so feel free to play a bit with the current app. To see specifics about each method, what it does and how it does it, you can visit the official Firebase documentation to find out.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now