Following Users Posts
Instagram Firebase
Now that we the capability of following users inside of our system, it would be nice if we improve our Home Feed to show their posts. In today's video, I'll go through a few examples of how this works by fetching posts for various users in the system and rendering it in our list. Finally, we go over how to sort our posts in a descending date order so we can see the newest items at the top. Once we complete this feature, I'll pull in an extension method on Date that returns a timeAgoDisplay string for our posts. If you're interested in learning more about the algorithm, please watch this video: Link: Time Ago Display Algorithm Enjoy.

Comments (22)
Hen Shabat
6 years ago
Hey Brian, Can we handle please in 2 main issues for the HomeController: 1. To fetch just 10 ( for example ) posts for the first time and that when you get the last post to load some more posts and so on. 2. To refresh HomeController by scrolling down. Thanks!!!!
Fred van Rijswijk
6 years ago
that would be a killer function, I tried it a lot on different ways...
Hen Shabat
6 years ago
Hi Fred, You solved that issue?
Hen Shabat
6 years ago
??
Brian Voong
6 years ago
Hen Shabat
6 years ago
Awesome! Thanks!!!
tomxue
6 years ago
At 9:31 of the video, I noticed the memory has gone up to somewhere around 365 MB. This is still well below the safe limit for newer devices, but for older devices, it's a little dangerous which probably would decrease compatibility, especially considering this user only follows a few people, what if somebody follows dozens of people and has a lot of images to load? Why has this app's memory usage gone so high?
Hen Shabat
6 years ago
Hi Brian, Why we don't have timestamp for follow? If we want to implement the activities screen we have to sort it and use timeAgo. Will you use timestamp for Follow or Like at the end of the course? Will you recommend to use Object oriented at this case? To create Activity class and that Follow and Like that inherit from Activity? Thanks a lot!
Hen Shabat
6 years ago
?
Brian Voong
6 years ago
mattnet
6 years ago
Thank you so much for the added bonus of the TimeAgo extension....saved a lot of time writing out my own function for that! Very nice!
jelaletdin123
6 years ago
How would you remove the gear icon, isn't it useless?
tobitech
5 years ago
It's settings, it provides options to logout of the application
johnrm9
6 years ago
Just for fun .. I added this to the HomeController - single tapping the navigation bar scrolls to the top and double tapping scrolls to the last cell or the bottom: fileprivate func setupNavigationItems() { let button = UIButton(type: .custom) button.frame = CGRect(origin: .zero, size: CGSize(width: view.frame.width, height: 40)) button.setImage(#imageLiteral(resourceName: "logo2"), for: .normal) // button.addTarget(self, action: #selector(scrollToTop(_:)), for: .touchUpInside) let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(scrollToTop(_:))) let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(scrollToBottom(_:))) singleTap.numberOfTapsRequired = 1 doubleTap.numberOfTapsRequired = 2 button.addGestureRecognizer(singleTap) button.addGestureRecognizer(doubleTap) singleTap.require(toFail: doubleTap) singleTap.delaysTouchesBegan = true doubleTap.delaysTouchesBegan = true navigationItem.titleView = button // UIImageView(image: #imageLiteral(resourceName: "logo2")) } @objc func scrollToTop(_ sender: UIButton) { collectionView?.scrollToItem(at: IndexPath(item: 0, section: 0), at: .bottom, animated: true) } @objc func scrollToBottom(_ sender: UIButton) { let lastItem = collectionView(collectionView!, numberOfItemsInSection: 0) - 1 let indexPath = IndexPath(item: lastItem, section: 0) collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: true) } Enjoy!
albaqawi
5 years ago
awesome thanks i will try it out
Maximilian Osborne
5 years ago
Nice one! thank you
rise9
6 years ago
Firebase fetchUserWithUID isn't working for me, and can't seem to find anything online about. Did they change the function?
Brian Voong
6 years ago
rise9
6 years ago
Thanks! Wow, not sure what happened. I finished Episode 21 and somehow landed on this one
Kritbovorn Taweeyossak
5 years ago
How can I make Year in func timeAgoDisplay() -> String { } ? . Sir
Brian Voong
5 years ago
edison1
5 years ago
This is sooo cool. I don't quite yet understand everything, but just seeing how this application has come together has been amazing.
wealthnut79
5 years ago
When I follow users i then can see their pics and comments in my home screen, however when i unfollow users i can still see their pics and comments. is this supposed to be like that or did i miss something along the way?
Brian Voong
5 years ago
stonypig1
5 years ago
this tutorial is great, watched it 2 times to fully understand it, this structure of firebase database, once we add an user A, then reload table, the user A entire posts: past old post and future incoming posts will all show up, I found a way only show up future userA posts in the main Home post page, which is adding a Timeline (extra node) in firebase for each user, put each user's own post did and his friend's post uid inside Timeline table, this way is used by many apps (WeChat), but when I loop through timeline to read those posts, I need to fire reloaddata() to reload table as many times as posts inside the timeline, anyone ever encounter this kinda problem ? or use this way to structure data ? please give some advice, or point some directions, thanks
stonypig1
5 years ago
just wondering, if we loop through 5 friends and each of them got 200 posts, total around 1000 posts, we will download all of them once we load the app, or only download data that we scrolled/show up on the iPhone screen ? anyone know? thanks very much
Aurélien Haie
5 years ago
There's something I don't understand, all my posts are dated from 1970-01-01 For saving the post in the SharePhotoController, I put the value : "creationDate": Date().timeIntervalSince1970 And for displaying the date I wrote in the HomePostCell file : let timeAgoDisplay = post.creationDate.timeAgoDisplay() and used it in the attributedText as it was mentioned in the video And so all my posts are dated from 629 months ago.. I don't see where the error is, even after comparing my project with the downloadable project (that actually works)
nando
5 years ago
Delete all previous images added to firebase before these last lessons and add new posts. The images that you have probably don't have a time stamp. Check firebase to confirm this.
Norlan
5 years ago
Hi Brian, I have this problem. I want to have a controller with the list of user following. Do you know how to do this?
AlfieLBTA
5 years ago
I wonder if making the call of Auth.Auth().currentUser.uid that many times is ok (money/requests quantity wise)? , wouldn't it be better if we store the user's ID on userdefaults and take it from there every time we need it?
lbta
5 years ago
mohit1007
5 years ago
Hi Brian, Question.. When I share a pic, shouldnt that also show up on home feed? Is it suppose to show or did i missed something?
Brian Voong
5 years ago
mohit1007
5 years ago
Yeah.. I found it out. This is awesome. I (we?) appreciate how much thought you have put into this course.
Jaylon22
5 years ago
Hey brian, I absolutely love this course, i just have a quick question, i am trying to make a list of the followers that some one has. I see that you where able to fetch the ids of the people you are following. Is there a way i can reverse the method and make a list of the followers like how you fetch the user for search and make a list of users. Thank you brian and have a bless day.
Brian Voong
5 years ago
jamescaampbell
5 years ago
Hey Brian, First of all. I love this course. Just wondering how could I modify the code to get all of the users posts not just the posts of the users were following.
Brian Voong
5 years ago
jamescaampbell
5 years ago
Thanks for the reply Brian, I can fetch all posts like this. But the username and profile images stay the same. Would do I need to change? fileprivate func fetchPosts() { guard let uid = FIRAuth.auth()?.currentUser?.uid else { return } FIRDatabase.fetchUserWithUID(uid: uid) { (user) in self.fetchPostsWithUser(user: user) } } fileprivate func fetchPostsWithUser(user: User) { let ref = FIRDatabase.database().reference().child("posts") ref.observe(.childAdded, with: { (snapshot) in print(snapshot.value) print(user.username) // self.collectionView?.refreshControl?.endRefreshing() guard let dictionaries = snapshot.value as? [String: Any] else { return } dictionaries.forEach({ (key, value) in guard let dictionary = value as? [String: Any] else { return } let post = Post(user: user, dictionary: dictionary) self.posts.append(post) }) self.posts.sort(by: { (p1, p2) -> Bool in return p1.creationDate.compare(p2.creationDate) == .orderedDescending }) self.collectionView?.reloadData() }) { (err) in print("Failed to fetch posts:", err) } }
jamescaampbell
5 years ago
Does anyone know why the username and profile picture stay the same?
drestaccz
5 years ago
Y is the user I'm logged in as picture are double up in home controller post
thenny chhorn
4 years ago
Hello Brian, this is the best course of Swift I've ever taken so far. It's really helpful to my project, but the app I'm currently developing has more contents others than just image cell. Say like facebook feed, there are some status and image as well. Do you have any suggestions where I start? thanks
Clint Larenz Nurse
4 years ago
Can I make it to where I do not fetch my own post, but only my followers?
Brian Voong
4 years ago
Clint Larenz Nurse
4 years ago
So I did that and I noticed my own posts kept showing. It was the edit profile bug where you can follow and unfollow yourself. I guess I may have clicked Edit profile and ended up following myself a couple days ago, and the buttons text changed back to " Edit profile" when I ran the app again . Just something I wanted to let you know in case you add any bonus features...
Cinquain
4 years ago
More fire!
HELP & SUPPORT