 Hello everyone. I hope you're doing well. In this video, I'm going to talk to you about the Firebase Cloud Firestore, which is going to provide the database services for our apps. Hopefully by now you have watched the Firebase kind of introduction, or excuse me, the Firestore introduction video, the official one from Google. If not, take a second and go watch that. It's quite short. But let me give you an overview of the Cloud Firestore and how we are going to use it and what it's good at. First thing I'm going to do is I'm going to make myself go away. So you can see all of the slides. All right. So what is this Cloud Firestore and why do we care? All your apps, or nearly all of your apps are connected to some sort of remote data source, right? Because for several reasons, first, if your app has data and those data are connected to users, right, you need to be able to share data within a single user across all that user's devices. And then, of course, many apps today, you know, think of your Instagrams, your YouTube, your Facebooks, your Snapchat, whatever, they're built around the premise of connecting people and sharing data between those people. Well, the way that works is you have to have some sort of remote data source, right? These devices are almost exclusively not talking peer to peer. You're not talking directly to another person's phone. You're going through a cloud service, a cloud database to share your information, right? But of course, you know, one of the other big uses of a remote database is to save things, you know, to keep them safe, to prevent against data loss, right? Phones are unreliable. They crash. Apps crash. So let's put the information somewhere where it is safe. It's also the case that your phone has somewhat limited storage. And if you're a picture junkie, you experience this on a daily basis. My wife hasn't had the ability to take a picture on her phone without first deleting a couple in a couple of years now, right? So putting the data off the device and up in the cloud has a lot of different benefits, okay? So cloud storage, what is the cloud? Cloud is one of those terms that's kind of floated around. And do we really understand what it means? Well, let's just try and distinguish between, say, a computer sitting in a closet at work that our devices talk to, right? So this is a server model, but it's not the cloud, right? We could write apps that talk and store data on this machine in our closet, but the cloud is fundamentally different, right? The cloud is huge, but all it is, what it really is, is a collection of these little server things just all piled all together for a couple of reasons, right? One is scalability, right? The cloud is huge. But again, it's just a collection of servers that host different applications that people can run their code on, but that can scale up to massive sizes, right? For example, Facebook has a cloud, Amazon's cloud powers Snapchat and a variety of other things. So the main thing is computing power and can you scale up to millions or billions of users? You can't do that with your computer in the closet at work. Reliability, the cloud gives you reliability. The cloud is not in any one place. It's not in Redmond, Washington or Canton, Ohio. It's spread out across the country, across the world, and the servers in the cloud are replicated across the world so that if, say, the internet connection to Wilmington is interrupted, then any cloud services that people rely on here, those fail over to some other safe place like Charlotte, okay? So you get reliability. And the other thing is you get efficiency, right? If I'm traveling to, say, Japan and I need to access my computer server at work, that's not going to be an ideal scenario. However, people like Google, Firebase, Microsoft, Amazon, they have data centers all over the planet. And typically when you connect to an application that's served in the cloud, you connect to the application that is physically closest to you because it is going to be the most efficient in terms of transmission time, okay? So the cloud is a collection of servers that are hosted by these companies. All right. Now, data and databases in the cloud. This slide is here for those of you who may have taken a database course or been familiar with databases. If you're not familiar with databases, you can kind of tune out for just a second. Most databases that people are familiar with are what we call relational databases. Things like MySQL, SQLite, Postgres, Microsoft SQL Server, those are relational databases. Relational databases organize, you know, your concepts that you care about into what we call tables. And then these tables are related to one another. So this structure has been around since the 70s. It's very efficient for certain types of querying and processing, but it's very inefficient for other types of processing, right? These types of relational databases were made with the idea in mind that data had something in common with one another. So what do I mean by that? Think about Amazon and how you have an order on Amazon. Your order, if you place an order on Amazon, a database record is created. And that order concept is also related to you, the user that bought it, potentially to the seller who sold it. Some price, some address where it was shipped to, ratings and customer reviews, videos promoting the product. The concept of an item is related to many other things. And the power of the relational database is being able to search or query across those relationships. So show me every order that Dr. Bob has made in the last 30 days that included the word batteries. This is what relational databases give you. And they're very powerful in that respect. But if you've dealt with a relational database, you know that designing them properly so that they can be queried and searched and updated efficiently is a challenge. So the more recent trend in database technology is toward non-relational databases or no SQL databases. The Google cloud fire, excuse me, the Firebase cloud fire store is one such thing. Non-relational databases kind of emerged from, I believe, from Google, actually, with the advent of the internet, where we're not really caring as much about the relationships between data in our database, because these relationships are expensive to maintain. All we're caring about is just putting information in and looking up that information later, not the relationships between information, right? So non-relational databases store things that are also called document databases. And you can think of a document as an instance of a class in Java. So like we've had the beer advisor and you had beers in that app. A beer, an instance of a beer would be a document in the database. And you don't really care about the relationships between beer and other beers. You just care about the attributes of that beer. That's what document databases are good at storing, right? And what makes them preferable to relational databases is they're much faster at storing this kind of just document and objects and attribute properties and then searching them with a full text search. But they're inefficient at searching for complex queries like show me all the orders that Bob made between January 1st and January 30th. That includes batteries. Non-relationals are not so good at that, but they're good at just looking up specific data points, okay? So some terminology. The Firestore, the Google Cloud Firestore, which is the database we're going to use, is a document-oriented database, okay? And there are some terms, and this is the basic structure of it. So first you've got a big database. Typically you're going to just have one database per app, right? So the document, these guys down here, the document is the basic unit of storage. And you should think of a document as one of the model objects in your model view view model pattern, right? A single document is a single instance of, say, a user object or an item for sale object or a beer object or a book, right? What it actually is, though, is just the data, like the strings and integers that comprise that object, stored in what is called a document, okay? So the documents consist of keys and values, and you can think of these keys and values as the attributes and the attribute values on your Java object. Now the beer has a color and it has a style. Those are both strings, so you would have style that's a string value, color that's a string value, et cetera, et cetera, okay? So those attributes in the documents are called fields, right? And they can include a whole variety of types, everything you're used to, integers, floats, strings. You can also have lists and a couple other things, like geographic points and some other things that we haven't encountered natively in Java, right? But the fields are the attributes of a document. So if a document is a class or an instance of a class, the fields are the attributes of that class, okay? The final thing you need to understand is a collection, right? So a collection are containers for related documents. So you may want to have a container, for example, of items for sale, right? You have a collection of items that are for sale throughout your app and each item has a different, or excuse me, each document in that collection represents a different item or users in your application, okay? So this is it. You got documents as the basic unit of storage, every document has fields that capture its data, and then the documents are organized into collections, right? And you have to have at least one collection, but that could be enough. You could have multiple ones, all right? So how do you work with the Firestore, right? So the Firestore is pretty clever because whenever data is added or deleted or modified in the Firestore database, in other words, you're putting data in or taking it out, it pushes the changes out to your device if you want automatically, right? So if one user changes data, like you say, this item has been sold and you save that to the database, you know, I on my phone say, oh, I completed the sale and I say it's sold, that data goes up to the Firestore. It can automatically push out that update to the sold item to every device on the planet. That's pretty cool. Or you can configure your application to periodically pull and then pull the changes every once in a while if you want to. But the good news is also you don't need to worry about networking stuff. Just like with the Firebase authentication that we talked about in the last video, you just call a Java library to put the data and get the data out and listen for updates. That's it. It's pretty simple once you get the, you know, the feel for the library. And one of the really, really convenient parts of working with the Cloud Firestore, which is not necessarily not so convenient working with other database services is that the Firestore allows you to map your documents to Java objects and Java classes directly, right? So you can say things like, I'm going to define a class in my app called beer, and it's going to have a string color and a string style. And then you tell the fires, you can create a list or an array list of these beer objects. And you can tell the firestore, hey, just save this beer object to the cloud and it'll do it. And you don't have to worry necessarily about translating it into this concept of a document or translating the field values or anything like that. You just say, put this beer object in the cloud and the firestore does it. Pretty neat.