Firestore Structure

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
3h 35m
Students
8
Description

In this course, we're going to create an Instagram Clone and learn how to work with cloud servers using Firebase. By the end of this course, you will be well-equipped to build your own apps!

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture, we're going to learn how to work with Firestore database so that we can get all of this information and save them to our database. So, let's go to our Firebase dashboard in which we can click on this database and we can initialize the database process. So, we're going to do this for once as we did in the storage and authentication. So, click on this database and you will see this Firestore option and we're going to go with Firestore. And in below you can see the real time database as well. But, we're not going to use this because that's old news. Firestore is the next generation of the real time database and it really has more powerful features compared to the real time database. So, we're not going to use real time database. We're going to go for Cloud Firestore, which is the latest and best technology available in the Firebase. I'm telling all of this because you may encounter some real time database examples in online tutorials in the Udemy or any other tutorial site like Youtube or the tutorials that you can see on the stack overflow you can encounter this real time database. But, we're going to go for Firestore because that's better and newer. So, if you create this you will be asked if you want to go for locked or test mode, we're going to start with the test mode. So, this is, the security rules that we have talked about in the storage. Default for the database is allowing, reading and writing for test mode and we're going to go for a test mode and then we can change it later on and as you can see for location we cannot choose anything because it has to be same with the storage. It makes sense to be same with the on the same location with storage because we're using the same database, it has to be synchronized. So, it doesn't let us choose another location at this moment. So, here you go. That's our database. Before we go on and use database in code we have to understand the database structure. So, if you have worked with SQL before, this is nothing like SQL. So, you have to forget about your SQL or sequel whatever you want to pronounce it. This is not like sequel. In here, we have collections and documents. So, for example, we have to start with the collection. If you click on the start collection, you can call your collection posts. And under the collection you will have a document. So, this will be your individual post. If you click on this auto ID, it will create a unique ID for your documents and in the document you may have whatever field and value you want. So, this is like a dictionary, like key and value pairings. For example in the posts we will have comments. They're going to be strings and something like I love this picture. I love this photo and let me add this so you can see it better. So, this is our collection. This is the main thing. This is like a class and under this class we have all these objects like posts and these are called documents in the Firestore. And when you click on a document you see its properties, you see its values. So, for example, we can have a user email and James@metallica.com value in here. And actually we're going to have the user email the comments and we're going to have a date as well. Let me just put a dummy value in here and then we're going to add another post later on for example, we can add as many as we want. So, they will have another value like lars@metallica.com. So, when I click on this now I'm inside the other post. If I click on this, I'm inside the previous post, I can add as many as fields as I want in here. So, this is the most basic representation of the Firebase data database, actually Firestore database. And you can have as many as collections you want. You can have as many as documents you want inside of a collection. And yet better you can have a collection inside of a document as well. So, if you want some layered structured you can have a new collection inside of this document as well and then another document under that collection. So, you can have layered collections and documents inside of Firestore. That's not very fit for our purpose right now because we just want to save the posts and we we will have as many as we want in here. So, this is currently meeting my expectation in the Firestore but, if I want to add another collection like users or I don't know like likes collection or something else then I can add it from here. And we're not going to do anything manual in the Firestore as well. I'm just doing this just to show you how this structure of the Firestore looks like. In order to let you understand the Firestore collections and documents we are going to do that all by, all by code inside of our X code. But, if you take a mental picture of this, if you remember this visualization, then you can actually understand what we are writing in the X code. So, you have to remember we have fill the values like a key and value pairing in here. In fact we're going to use dictionaries to supply this information to the Firestore. And that's what we're going to do in the X code. So, I'm going to delete this collection from here and we're going to start from scratch so, that you will see we don't have to do anything manually inside of this database. Once you initialize this database, then you're good to go to come here and write your actual codes. So, remember we're using Firestore so, we can come over here to Cloud Firestore documentation, not the real time database and click on one of these tabs like get started or structure data add data, delete data, retrieve data, whatever you want. You can find any documentation in here as you wish. I really suggest you take a look in here as well because I'm going to be using the most common techniques inside of our app, but of course I cannot tell you everything about the Firebase because it has so many features. So, let me refresh this and in order to leverage those features in your apps, all you have to do just come here and take a look of course after understanding the basics. Here, you see some sample codes for different kinds of programming languages. Obviously, we want to see the ones in Swift and don't worry, I'm going to show you how it's done and I'm just showing you this is as an example so that you get the habit of reading documentation before you go in an operation like this. So, we're going to do what we have been doing so far. We're going to create a Firestore database variable and this will drive from the Firestore Firestore database as you can see .Firestore. So, this is going to be our instance of the Firestore class. And later on we're going to use this to download and upload data. So, next thing to do is just to create a reference as well, but this will be a little bit different than before. So, we're going to call this Firestore reference and this will be actually a document reference type. So, put a column not equal sign because I'm going to use this document reference to uploads and download data as you can see read, write, listen data. And I'm going to make this optional and in fact I can make this nil to begin with because after this I'm just going to initialize this. I'm going to initialize this using Firestore database. So, we're going to do something like this. Firestore reference is now Firestore database. Firestore database.collection. So, it will ask me for a collection name. So, that's where I want to give posts. So, after you create your connection, you choose your collection name. You can say add documents and in here it asks you for a string to any dictionary and choose the one with the completion block because as usual we want to see what happens after we do this. So, here we have to provide a string to any dictionary because remember in the database in the field and values, we have value key pairings. So, that's what we're going to do. We're going to create key value pairings with dictionaries and supply all of this information with dictionaries to Firestore. So, that's why it asks us to create string to any dictionaries. And after that it will give me a completion. And in the completion if you hit enter, you will only see an error. And of course we're going to check to see if error is not nil. If error is not nil we're going to display an alert message to the user. And else if it's nil, we're going to do what we have to do. So, since we have our make alert function, let's call this self.makeAlert, title input will be error, message input will be error.localizedDescription. Of course, it doesn't auto complete right now because I haven't taken care of this problem in here. But I believe I have written it correct. So, we can check it later on. So, let's take care of this String : Any dictionary so that we can proceed. So, in the String : Any dictionary, I want to save all my related information, right? So, I'm going to call this Firestore post. I'm going to open this braces in here, and we're going to write the first key, first field name as imageUrl, right?. So, if you put a column, the imageUrl value will be this imageUrl. So, let's do imageUrl and put an exclamation point in order to make this not optional. Of course, you can check for if let in here, but I believe this will be okay. And later on, after the imageUrl what do we have? We have the name of the file, name of the owner of the file. So, we have the user name and we have the comment. So, I'm going to call this postedBy. So, I'm going to select whoever posts this. So, who posts this? Of course, the current user using our app is going to post this. And we can get this user by saying Auth.Auth.currentuser, as you might remember, okay? But we're not done here. After that, we have to get the email address of this user. So, I'm going to say email. So, that's it. That's how I get the current user. So, let me close this side panes so we can see it better. And after the imageUrl and postedBy, so what else do we get here? We have to get post command as well, right?. We have a comment in here in the self.commentText. So, let me write this as well. I'm going to call this postComment, and the value of this postComment will come from self.commentText.text. And I can unwrap this as well. So, finally we have to give it a date, and actually not finally. We have to give it a date and then likes because we're going to have likes option in our app as well. And for dates, I'm just going to say date right now or leave it empty. We're going to see how to do that later on, okay? I'm just going to say date right now. And for likes, I'm going to start with zero and then I'm going to add one after somebody likes our post. So, I'm going to say zero over here. So, once I do that, this will ask me to cast this as a string to any because we're having a lot of different objects as values. And even though it didn't ask, I have to cast this as String: Any anyway, because this Firestore reference method asks me to give an input of String : Any, anyway. So, whatever you write here as value, you have to cast it as a string, Any. And after that you can just say firestorePost in here, and we're getting some error here. It says that 'firestoreReference is a constant and you're changing it'. So, let me hit 'Fix' to make it var, or you can just come over here and write var as well. And in here it asks for an optional or force unwrapping value in here. So, I'm going to go for optional and later on you can provide the default value if this is an empty value here saying ?? "Error". So, I believe now this is okay because we have created our Firestore post. We have created our dictionaries, String : Any dictionary and by using this Firestore reference, we are uploading that information in the posts collection, okay? So, now I believe we are ready to test this. Later on, we're going to change this date to the actual date. But for right now we can actually test to see if we can in fact provide this imageUrl posted by postComment to the server or not. So, let's open our simulator. Let's go to Upload section, okay? Let's select a picture so that we can upload it to the storage, and let's write a comment this time because we're going to test this. So, let's say Starry Night. And if I hit 'Upload' it won't take me to the feed. This is an expected behavior because we haven't written that code yet. But we can come over to the database and refresh it to see if we have the posts collection created. So, here we go. We now see the posts, we now see the document, and under this document we see the date, imageUrl, likes, and all the other attributes, all the other field and values that I have defined in my post dictionary. So, here in the imageUrl I see the current uploaded imagesUrl. So, that's good. That's all I wanted to do. We have to take care of this date in order to order all of this information, order all of these posts by date in our feed. So, let's stop here and do that within the next lecture.

 

About the Author
Students
422
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics