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.
Hi. Within this lecture, we're going to finish our like function. We're going to use this like label and document ID label in order to get related information for our like function. So, all we have to do is just increase these likes by one and upload new value to the server. In order to do that, of course, we're going to import Firebase in the FeedCell as well. And in the likeButtonClicked function, I'm going to call this fireStoreDatabase like we did before. So, it will drive from the Firestore.firestore. This is going to be our instance. And now I can come over here and say fireStoreDatabase.collection. And collection will be posts. And if I say .document, it will ask me for a documentPath. So now, if I get one of the document IDs like this one, and if I put that document ID in here, whatever I do next will be related to only this document. So, if I work within this document, if I try to upload new value, it will be only uploaded to that document only. So, that's how you filter. That's how you limit your operations. And in order to get this, of course, I'm going to use the documentIdlabel that I have just created. So, as you can see we can do documentIdLabel.text. Now, if I say something like .setData or getData, it will only bring the current values of that document or it will only upload data inside of that document. So, before we do that, I'm going to take the likeCount and I'm going to do this with an if let because we're trying to convert something into an integer. So, you will have to say IntLikeLabel.text. And this will bring us the current likeCount. So, let me cut this and paste it inside of this if let. And inside of this if let, we have the current likeCount and we want to increase it by one and save it inside of our fireStoreDatabase. In order to do that, of course, we have to come up with a string to any dictionary, like we did before. Even though we're going to upload only one value, it asks us to create a string to any dictionary and that's what we're going to do in here. So, let's call this something like likePost or likeStore or I don't know, likeDictionary, whatever you want. And it will have the likes as key. And for the value, we're going to have to use likeCount, but we will increase likeCount by one. So, it will be likeCount plus one. And we have to convert this as string to any. So, this is good. We're casting this as a string to any dictionary and we're going to set this to be a data inside of our this fireStoreDocument. If you say setData, you have to find the option that has the merge parameter. So, merge means we are uploading this likeStore, likeDictionary, but we're merging with the existing values. So, we're going to update the likes but we're not touching the postComment, date, imageUrl. If you don't do this, if you don't say merge, it will just delete the other values other than likes, and you will end up with a broken post, broken document inside of your database. So, make sure you say merge true and this is how you update a database value without breaking the other values inside of your Firebase FireStore. So, let's try this, let's open our simulator and let's see if we can like this picture. So, as you can see the current like is zero, if I hit 'like' it will increase it to one and as you can see, changed. So in here, I see the likes as one as well. So, it's working. I can like this picture, I can like this picture as well and let's try the last one. So here you go, our like function is in place. So, of course we have a kind of let down in here. If I hit like more than once, it will just like it more than once. And even though we are the same user in here, we are the post owner and we are liking of our own picture we get to like as many as we want. Of course, there are alternative ways to implement like function, like, we can create another collection like something like likes and inside of that like function we can store which user liked which document. So, we can keep track and we can say that if this user has liked this document before then don't increase the count. So, we can have that kind of controls in real apps but I'm just implementing this like function as an example to show you how to use updates, how to use document Ids, and how to use merge so that you know how to work with Firebase. Once you learn how to work with Firestore you can do whatever you want. You may have more than 10 collections. You may have more than 1000 documents. Maybe you may have a complex structure but right now we're going to do what we have to do to understand how to use Firebase. So, let me create another user here to test if everything works fine. So, let me try to like these posts by James. As you can see, it still works. So our app can work with multiple users as it's supposed to and we can see and like the other user's posts as well. So, if you want a follower collection here, for example, you may have one and you can actually save the information who is following who so that you can only add a filter when you try to retrieve information from Firebase and they can only see the pictures of the users they are following. So, for example, let me upload a new picture in here and this will be seen by Lars and James and all other users in our app. So, if you want to implement a follower, you can do that with Firebase. If you want to implement a better like feature, you can do that with Firebase. If you understand how this works, you can do whatever you want to do with your app. So, this is a very powerful tool. I'm sure you will understand it better when we do other clones as well. So, make sure you keep up with the course and try to implement these features on your own while following this. So, let's stop here and talk about security in the next lecture.
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.