At the core of modern mobile application databases is a thorough understanding of Firebase. The Firebase Database is an incredible product, but there’s still a big opportunity for improvement. The Firebase team listened to the feedback from the developer community, and Firestore is their answer. In this course, Firebase on Android: Cloud Firestore, you will learn how to master the new flagship database for Firebase. First, you will learn how to structure data following Firestore's document-oriented design. Then, you will see how to create, retrieve, update, and delete data and images. Finally, you will explore querying individual documents or entire collections with only a few lines of code. When you’re finished with this course, you will have a foundational knowledge of the Firebase Cloud Firestore that will help you as you move forward to develop mobile applications.
Course Overview My name is Mitch Tabian, and welcome to my course Firebase on Android: Cloud Firestore. I'm a self-employed Android developer, and I'm here to teach you about Firebase's new flagship database, Cloud Firestore. Cloud Firestore is essentially an upgraded version of the Firebase database. In a nutshell, there's a lot of things Firestore can do that the Firebase database can't, but there's nothing the Firebase database can do that Firestore can't. The Firebase database is an incredible product, but there's still a big opportunity for improvement. The Firebase team listened to the feedback from the developer community and Firestore is their answer. Some of the major topics we'll cover include structuring data. Firestore has a unique document-oriented design that I find extremely intuitive. Compared to object-oriented programming, you can think of objects as documents, and lists of objects as collections. Creating, updating, and deleting data. We'll insert documents into collections, insert documents into sub-collections, update individual fields in documents, and delete documents. Reading data. With Firestore, you can query individual documents or query entire collections of documents. These queries can be executed with only a few lines of code, and automatically operate on background threads. By the end of the course, you'll have an excellent understanding of how you can use Firestore to manage all your application data. Before beginning the course, you should be very familiar with Android Studio, and I recommend watching my Firebase on Android series on Pluralsight. I hope you'll join me on this journey to learn about Firestore with my Firebase on Android: Cloud Firestore course at Pluralsight.
Integrating Cloud Firestore Before we move forward, I want to remind you that this is not an introduction course. This is the fourth course in a series. For that reason, I won't be going through a lot of the Firebase setup procedure like I did in the previous three Firebase on Android courses. I'm not going to be walking you through how to create a new Firebase project, registering the app with Firebase, or walking you through how to add the Google-service. json file to the app folder of your project. Remember, if you need a review on how to add the Google Services JSON file to the project, watch the clip named Syncing the Android Project with the Firebase Project in any one of the other Firebase on Android courses preceding this one. So moving forward, I'm going to assume you have the source code, it compiles and runs, and you're able to use the Tabian Consulting application.
Inserting and Managing Data Cloud Firestore is a NoSQL document-oriented database. There are no tables or rows. Firestore has a unique design where data is stored in documents. The documents are organized into something called collections. Each document contains key value pairs. They can be strings, numbers, Booleans, objects, arrays, timestamps, geopoints, references, or, of course, null. As you saw in the application demo, we're building an issue-tracking system. At the top of the hierarchy are projects, then there are issues within those projects and attachments within the issues, so it goes projects, issues, attachments. So intuitively you'd expect to see a list of projects. If you selected a project, you would expect to see the properties unique to that project, and then the issues within that project, which is exactly what we see here. If I click a project, there's the properties, otherwise known as fields, then inside the project I selected there's a collection named issues. If I click on issues, I see a list of all the issues within that project, so if I'm using the correct Firestore terminology, there's a list of issue documents. If I click on one of the issue documents, I see the unique fields associated with that issue, and if that issue has attachments I see another collection named attachments. And if I click on the attachments collection I see a list of attachments. I'll click on one of the attachment documents, and there's where our road ends. We have the fields associated with the attachments, but no more collections. To me, a system like this is very intuitive. I love it. It's very simple, and I find it very effective. I don't want to talk too much about technical details until we look at some examples, so without further ado let's insert some data.
Querying Data Querying data with Firestore is very similar to querying the Firebase database. Essentially, you create a reference to the document or collection you want to get, and then initiate the task and check for completion with an onCompleteListener or an onSuccessListener. My overall impression of Firestore is that it's essentially a better version of the database. There's a lot of things Firestore can do that the Firebase database can't, but there's nothing the Firebase database can do that Firestore can't. I had almost no real difficulties when first exploring Firestore, but I still remember the first time dabbling with the Firebase database. Inserting and reading lists of data was especially problematic. With Firestore collections, I had no issues with lists.
Updating and Deleting Data Just like the Firebase database, updating data with Firestore is pretty much the same as inserting it. There's only a few subtle differences. Firestore has an update method that you can use on a DocumentReference. For example, suppose we wanted to update a single project. We'd get the DocumentReference, then call the update method and specify the fields we wanted to update. Other than that, you can also just use the set method like we did when first inserting data. Deleting data is a straightforward process. For example, if we wanted to delete a project document it would look something like this. Create a DocumentReference to the project, call the delete method, and then attach an onCompleteListener.