The course is part of this learning path
In this course, we're going to create a Snapchat clone using structs and various advanced techniques of Firestore.
Learning Objectives
- Learn how to make a Snapchat clone
- Understand all the features of Snapchat and how to set them up
Intended Audience
This course is intended for anyone who wants to learn how to develop apps on iOS.
Prerequisites
To get the most out of this course, some basic knowledge of iOS development would be beneficial. We recommend that you take this course as part of the Developing Mobile Apps for iOS learning path.
Hi, within this lecture, we're going to create our user model. So, what do I mean by that? We have to upload the user name to the FireStore when we hit the upload button in our Snapchat Clone. So, let me come over here. As you can see in the feed view controller, we're going to get the data from FireStore, and in the upload view controller we're going to upload the data to the FireStore. But in the upload view controller, I also want to upload the user name. Who made this snap? Who made this post to the FireStore? And in order to get that, I'm going to get it in the feed view controller so that I can share it with my other view controllers as well. So, I don't want to run a Firebase query in every view controller. It wouldn't be efficient. So, I'm going to get that once in the feed view controller and I'm going to share it with other view controllers. So, that's what we're going to do within this lecture. So, let me import this Firebase in the feed view controller. And I'm going to get this under a new function because we're going to get a lot of things inside of our feed view controller, and getting user info is just one of them, so I'm going to write this under a function. So, under this function, I'm going to do a very simple thing. First, I'm going to create my fireStoreDatabase variable. So, this will be FireStore instance as usual. So, fireStore fireStore. And under getUserInfo, I'm going to use this database to reach my UserInfo collection, and here actually, I'm just going to get the datas. So, right now I have only one document, of course, I will have like a 1,000 of them when this app is released. So, I'm going to filter my results with email. So, I'm just going to get the data who matches my email. So, that's how I get the user name. And by the way, we don't have to add a snapshot listener in this case, we can't just get this data once. Remember, when we want to check for updates, we use this addSnapshotListener. But this time I don't need to listen for changes, it won't even get changed. So, I can just use this .get document to get this for once. So, remember this get documents? Now we're going to use it. So, first of all, let me filter this, and the field that I'm looking for is the email, and this will be equal to my signed in users email. So, Auth.auth.currentUser and I can force unwrap this because I'm signed in right now, and say .email. So, later on I can just say getDocuments and this will give me a snapshot block and let me close this down. And once you hit enter on that, you will either get a snapshot or an error. So, now while we're here, if error is not nil, we're going to display an error message to the user. And if error is nil, then we're going to go ahead and process this snapshot to get the user name. So, let's do that if error is not nil, so else, and I believe we have to go back and find the make alert function in order to speed things up. So, come over here, copy this, come below and paste it so that we can use it over here. Okay? If error is not nil, I'm going to display an alert message to the user. So, this will be error, this will be error.localizedDescription, and we're going to say error. So, if that's the case, if error is nil, then I'm going to check to see if snapshot is empty false, and if snapshot is not nil. So, it means that I really have a snapshot because over here I'm going to say for a document in snapshot.documents, and I'm going to force unwrap this, and remember this gives us a snapshot array so that we are just looping through this array. And once we iterate that array, I'm going to say document.get and I'm looking for the field user name. And if we say user name, we can just cast this optionally as a string, and of course I'm going to do if let in order to get that. So, that's easy. That's how we get data from the Firebase and we already know about this. But right now how I'm going to transfer this information to the other view controllers? That's the question. So, in order to do that, I'm going to create another group which is called model, and I'm going to create my model in this folder. So, I'm going to go ahead and create a new Swift file by right clicking over the Model folder, and I'm going to say File and choose 'Swift File' because we're going to create a class rather than a Cocoa Touch class. So, I'm going to call this something like User Singleton. We could have just called it user, but I believe there are other classes called user, so I don't want to make things complicated. Okay? So, I'm going to say class UserSingleton and open curly braces. For some reason, if you haven't watched the Foursquare clone section, then you wouldn't know what a Singleton class is. It is a class that you have only one instance. So, nobody can create any instance from this class because we're going to have our initializing function as private, so that nobody would reach and create an instance of this class, but rather, we're going to create this instance ourselves so that we can reach it from anywhere we want during this project. So, I'm going to create a static variable called sharedInstance, and of course, we can call this something else. we have used this shared instance term before. I'm going to call this sharedUserInformation. Not now so it''s shared UserInfo, so this will be our only UserSingleton instance, so that we're going to set some values and we're going to get this exact same values in other view controllers. And we're going to have an email and the user name in this case. So, we're going to change this email and user name and we can get the same values from other view controllers. So, that's what we have used in the Foursquare clone. So, if you haven't watched it, now you know how to work with Singleton classes. So, let me do a command B and come over here, so I can say userSingleton.sharedUserInfo.email. So, this will be the email of the user. Of course, you can use Auth.auth.currentUser.email in order to get that. And you can go for userSingleton.sharedUserInfo.username, is the username that we have just created. So, here you go. Now we have defined our username into a Singleton class. So, we can come over our upload view controller and we can easily reach that data over here. I'm not even going to test this because we have done it before, because in the next lecture we're going to upload our picture to the storage anyway, and we're going to see if that works or not. If it doesn't work, of course, we can go back and revise our code. So, let's go to the next lecture where we will upload our data.
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.