On Android, How to Use Firebase Database

Are you interested in learning how to use the Firebase Database on Android? You've arrived to the right location.

Firebase Database

When I talk about databases, this is what happens to me.

You should be able to answer the following questions by the end of the guide:

  • What is the Firebase Realtime Database and how can I utilize it?
  • How to use the Firebase Realtime Database to perform queries.
  • What is the best way to structure a Firebase Realtime Database?
  • What is the Firebase Realtime Database and how does it work?
  • How to get data from the Firebase Realtime Database in Android.
  • How to read data from an Android Firebase Realtime Database.

What is Firebase Database?

Firebase Database is a cloud database that you can use in your mobile and web apps. Using Firebase's APIs, you may save and retrieve data from the Firebase Database.

With an example, I'll show you how.

There are two types of databases that Firebase Database offers:
  1. Real-time database powered by Firebase
  2. Cloud Firestore by Firebase
Both are NoSQL databases that are efficient, scalable, and dependable.

Firebase Realtime Database preserves data in JSON objects and makes it available in real time to all connected clients.

Firebase Cloud Firestore saves data in the form of documents and collections, and it syncs data in real time with connected clients/devices.

For demonstration purposes, you can use both of these databases in an Android app.

What is Firebase Realtime Database, and How Does it Work?

To put it another way, Firebase Realtime stores and retrieves data using JavaScript Object Notation JSON, which means there are no tables or structures like SQL, only keys and values, which might contain additional keys and values, and so on.

Other required features provided by Firebase Realtime Database include defining rules for your database, creating a backup, and analyzing usage.

A few words on JSON Structure

All of the data in the Firebase Realtime Database is kept in a large JSON tree, which can contain JSON Objects, Arrays, and values, among other things. JSON Objects always begin with “” and end with “,” as in:

JSON Example
The following image comprises a JSON object, which begins with (left braces), and within that JSON Object is another JSON Object titled "demo," which has the value 123 assigned to "Test" and "Value 2" allocated to "Test 2." This is how JSON works, and it's how the Firebase Realtime Database stores data.

Let me illustrate with an example. In Firebase Realtime Database, I've built code (described below) that creates a database similar to this.

What Do Realtime Database Rules Mean in Firebase?

After all, Firebase Realtime Database is a database, and its security should be taken into consideration.

As a result, Firebase offers a simple user experience in this regard. In the Firebase Realtime Database pane, there is a tab labelled "Rules."

Simply open that tab and type the rules however you like. Here are some examples of rules:
  • Default Policy - By default, all rules are set to prevent anyone from reading or writing to your database. Only the Firebase Console may access the database.
  • Public Law - It implies that anyone can gain access to your database. It is dangerous, and you should not use the product if these restrictions are followed. They must be updated before going into production.
  • User-defined rule - This is a common case in which users only have access to their personal information. When a user is authenticated with Firebase Authentication, it is assigned a UID, which is a unique identifier. And whatever data that the user generates is saved under that UID node. As a result, the following rule ensures that each user only has access to his personal information.

How to Connect An Android Project to Firebase Realtime Database?

So now you have a basic understanding of the capabilities and structure of the Firebase Realtime Database.

It's now time to begin writing and reading data to Firebase Realtime Database.

First and foremost, you must connect your Android project to Firebase, which necessitates the use of a Google account.

The first is manual, which entails manually adding the dependency to Android Studio and manually adding an app to Firebase Console.

The alternative option is to use a plugin, which is offered by Firebase and comes pre-installed in the most recent versions of Android Studio and performs everything for you.

We'll use the plugin, but if you want to learn more about how this plugin works, you should connect manually.

Connecting your app to Firebase is a simple process.

This is How You can Integrate Firebase Into Your Project,

Step 1 - To begin, open Android Studio.

Step 2 - Make a whole new project.

Step 3 - Choose from any of the pre-made templates; I'm going with Empty Activity. Next should be selected.

Step 4 - For demonstration purposes, I am leaving everything default in the App Name, Package Name, Save Location, Language, and Minimum API Level fields. Finally, press the Finish button.

Step 5 - A new project for you will take a few moments to complete. When you're finished, go to the Tools menu and choose Firebase.

Step 6 - It will open a right-hand pane with a list of Firebase Products, but we're only interested in Realtime Database, so we'll expand it and select "Save and retrieve data."

Step 7 - Now you'll see "Connect your App to Firebase" with a button that says "Connect to Firebase"; click it. If you aren't signed in with your Google account, Android Studio will redirect you to Chrome (or any other default browser) to log in.

Step 8 - After you've logged in, you'll be asked whether you want to connect your app to an existing project or start a new one. After you've decided on one of them, click Finish. Android Studio will now sync your project and apply Firebase requirements. It could take a few moments. Your project will be connected to Firebase after it is finished.

Connecting to Firebase Realtime Database is simple if your project is already connected to Firebase.

The assistant pane, where you connected your app to Firebase, has a button that says "Add the Realtime Database to your App." Simply click it, and your app will be connected to the Firebase Realtime Database of the project you chose/created for this app.

Data is Being Written to the Firebase Realtime Database

You may now begin writing code to save and retrieve data from the Firebase Realtime Database when everything has been set up.

The Database instance is the first item you'll need to do any kind of activity. This is very simple; simply write the following code and you will have a Firebase Realtime Database instance.

//Getting Firebase Instance
FirebaseDatabase database = FirebaseDatabase.getInstance();

You'll need a reference to store your data now that you've created a database instance.

A reference can also be thought of as a journey. You may get the reference to the root node of the entire Firebase Realtime Database — the one at the top of the JSON Tree – by using the following line of code.

//Getting Database Reference
DatabaseReference databaseReference = database.getReference();

You can pass any node name to the aforementioned getInstance() method, and it will return its reference if that node exists. If not, a new node will be formed using the specified string parameter.

You can begin writing your data after obtaining the reference to the location where you want to save it. Data can be written to a reference in a variety of ways.

Firebase Realtime Database vs. Firebase Cloud FireStore: What's the Difference?

Firebase Realtime Database Firebase Cloud Firestore
Firebase provides an older database model. Firebase has introduced a new database model.
JSON Tree is used to save data. To save data, it employs a document collection paradigm.
When you retrieve one element, you receive everything underneath it as well. By default, queries are shallow. You only obtain the document when you fetch a document from a collection
When you scale up, it becomes more difficult to control. When the database matures, it is easier to manage than a real-time database.

What is the Document Collection Model?

Documents are JSON objects that include key-value pairs that can be used to store almost anything. Fields are the name for these key-value pairs.

Where the collection is essentially a document collection.

There are 4 rules of this model:
  1. I mean it when I say the collection is a collection of documents. Documents are the only thing that can be stored in a Firestore collection.
  2. Documents in Firestore cannot exceed 1MB in size; if they do, you must break them up.
  3. A document cannot contain other documents, although it can have sub-collections, which can contain documents. However, no document may be saved directly into a document.
  4. The Firestore's root can only hold collections. So, if all you want to do is save "Hello world," you'll need to first build a collection, then a document, and then a value for that document.

Post a Comment

Previous Post Next Post