Fetch Posts User Profile
Instagram Firebase
Now that we have a firm understanding of how to fetch posts, we can implement very similar logic to fill out our user's post history inside their profile page.

Comments (29)
Volkan Gürol
6 years ago
Hi Brian, thanks for great tutorial. It may be out of scope but, instead of loading all pictures pagination would be great example.
Brian Voong
6 years ago
Fred van Rijswijk
6 years ago
It's a great option, also The Who to follow block which you mentioned in a Youtube live video ;-)
Hen Shabat
6 years ago
Hi Brian, How are you? Can I ask what you think about implement the facebook messenger idea in this instagram app? I thought only about add another screen to the mainTabBar and over the to show the current user open chats. Another question please, How to save the record on Firebase? like we worked with "posts"? or just follow your guide? Thanks!!!
tyehowatt
6 years ago
Hey When i try to load the profile image it prints the profileImageUrl but doesnt set the image on screen whats going on here?
Brian Voong
6 years ago
tyehowatt
6 years ago
I worked the bug out, thanks for the quick reply :)
fahad
6 years ago
Hello Brian! I have a question regarding the fetch of the posts...I'm doing a slightly different implementation to the posts. I have a users node and a posts note, but not the same as yours. My users node is slightly the same as users, However it does contains an extra child name Media that has the user's media, but in a childByAutoId form only (Key ids)..and my post node looks like Posts -->childByAutoId --> posts details..I know i kinda messed it up, but is there a way to access the current user media and fetch them in the profile section? Thanks a lot and sorry for the long question.
Brian Voong
6 years ago
fahad
6 years ago
Indeed. Thanks a lot, Brian. Wish your dad best of health. Take care and enjoy your short break.
smiller193
6 years ago
Is anyone having an issue with the input accessory view getting stuck at the bottom? When exiting comment screen
fahad
5 years ago
Good morning Brian, i'm having an issue fetching my feed upon posting. It works good in the profile, but when check my feed nothing shows up, unless if i pull to refresh.. P.s I have a multiple feed and i'm subleasing the feed cell 3 times (So i have 3 feeds instead of one). Thank you so much
Brian Voong
5 years ago
PatrickVB
5 years ago
Good Day! Do you have code for pulling other Firebase Data beside the imageUrl? And injecting it into a tableviewcell?
PatrickVB
5 years ago
I figured out how to do what I was asking! Your tutorial are great! I just have to watch them a couple times to get a Full Understanding!
Laurent Maquet
5 years ago
Hi brian, Thank you for the great job ! Could you share the way to implement a progress bar for all heavy network tasks ? Or Give us a link ? Is there a special firebase call back to get the actual progression all along the task ?
Brian Voong
5 years ago
Laurent Maquet
5 years ago
Many thanks Brian !
Tokyojogo
5 years ago
Hi Brian, After doing this part of the tutorial, I noticed that after adding a new post and the sharePostController gets dismissed, it doesn't really refresh the userProfileController so the new pic added doesn't get displayed. Will this be addressed in later vids? If not, how do we go about this? is it because we're only observing a singleevent?
Brian Voong
5 years ago
xn930uio
5 years ago
Hey Brian, Thank you for your tutorials. I was wondering if it better to have a struct or a class when using downloaded Firebase objects. I understand that classes are references and structs are just copies so if I were to make a social media App like Instagram, and if I were to have lots of users, what's the better way: structs or classes?
Brian Voong
5 years ago
Ashim Dahal
5 years ago
I am working on one project where I need to call multiple REST Apis to get all the value in that controller. What is the proper way to set up a model in such scenario? There is no single apis that holds all the data... any suggestion on this?
Brian Voong
5 years ago
dshawn
5 years ago
Hey Brian, I have have come across a problem that I can not figure out. I have my fetch post set up like this: guard let uid = Auth.auth().currentUser?.uid else { return } Database.database().reference().child("photoPosts").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in print("the photo was called", snapshot.value) guard let dictionaries = snapshot.value as? [String: Any] else { return } dictionaries.forEach({ (key, value) in guard let dictionary = snapshot.value as? [String: Any] else { return } let post = photoPost(dictionary: dictionary) print("image url from photopost struct", post.imageUrl) self.posts.append(post) }) self.collectionView?.reloadData() }) { (err) in print("Failed to fetch ordered posts:", err) } When I print the snapshot value, the debug console shows all the information from firebase, however when I print just the imageUrl I get nothing. Could you please let me know what I am doing wrong? (i also have the dictionary set up to receive the image url as well)
dshawn
5 years ago
Here is how I have my struct setup import Foundation struct photoPost { let imageUrl: String let caption: String let signature: String let creationDate: Date let imageHeight: String let imageWidth: String init(dictionary: [String: Any]) { self.imageUrl = dictionary["imageUrl"] as? String ?? "" self.caption = dictionary["caption"] as? String ?? "" self.signature = dictionary["signature"] as? String ?? "" let secondsFrom1970 = dictionary["creationDate"] as? Double ?? 0 self.creationDate = Date(timeIntervalSince1970: secondsFrom1970) self.imageHeight = dictionary["imageHeight"] as? String ?? "" self.imageWidth = dictionary["imageWidth"] as? String ?? "" } }
dshawn
5 years ago
Got it to work! I figured out what I was doing wrong. You can ignore this question. Thanks.
Krubel Habteyes
5 years ago
I am not getting red cells to appear. The addsubview is nor working. Help please.
Brian Voong
5 years ago
Krubel Habteyes
5 years ago
I downloaded your project but it didn't work cause of the podfiles. This is my code for UserProfilePhotoCell i am not sure where the error is: import UIKit class UserProfilePhotoCell: UICollectionViewCell { var post: Post? { didSet { print(post?.imageUrl ?? "") } } lazy var photoImageView: UIImageView = { let iv = UIImageView() iv.backgroundColor = .red iv.contentMode = .scaleAspectFill iv.clipsToBounds = true return iv }() override init(frame: CGRect) { super.init(frame: frame) addSubview(photoImageView) photoImageView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
mohit1007
5 years ago
remember to register the cell in the uicontroller viewdidload function
mrnickegg
5 years ago
Hey Brian, Do you have any tips on how to download a video from Firebase storage? I have successfully found out how to upload videos to firebase but downloading them back in the profile feed is another challenge. Looking to download in my ProfileVideoCell.
Brian Voong
5 years ago
esong2288
5 years ago
How come you don't have to use the dictionary.forEach when you're observing .childAdded.
chrisggg
5 years ago
Hi Brian, thanks for the courses. I have little problem. After I finished this video, and run the project, I still keep getting the red box on User Profile page. When I removed the red background color in PhotoImageView(from UserProfileCellPhoto.swift), the red box is gone but still no pictures that I've uploaded. I've copied and pasted every code from the sample project into my files, UserProfiles and Posts files, and the picture still haven't rendered. I think imageURL link isn't passing through.
chrisggg
5 years ago
I managed to solve it! in Post.swift, better make sure "imageUrl" is spelled the same as the file in SharePhotoController.swift's saveToDatabaseWithImageUrl. In Post.swift self.imageUrl = dictionary["imageUrl"] as? String ?? "" In SharePhotoController.swift, saveToDataBaseWithImageUrl let values = ["imageUrl": imageUrl, Again, make sure "imageUrl" are spelled the same in both files. previously, I had imageUrl spelled as imageURL in Posts.swift
Rickon Zhang
5 years ago
Hi brian I have a question. If the profile has 10000 post, how to get the 10000 post? And if restart the app, will reload the 10000 post ? Did we need to store in local (in iphone )?
Brian Voong
5 years ago
mustii
5 years ago
Hey Brain, I was wondering why are you getting the images from the cell instead of the controller? aren't we supposed to separate concerns? Thanks in advance
ubi
5 years ago
Hey Brian, You mentioned in the previous episode about your wish that UIImageView comes with content mode as scaleAspectFill and clipsToBound to be true. I am fairly new into this iOS development, in fact, 0 code written so far just watching your video first. But I would suggest for future videos to actually create your own ImageView that extends from UIImageView but it has those values set up by default. That is what I would do since it helps in the long-term and people could get the heck around OOP and cleaner code. Cheers, and I love the course, really helpful!
paul18
5 years ago
Hi Brian, I have 2 views that are both returning posts however it appears that my didSet is not firing on one of the views. I have debugged the fetchPosts and the data is entering the dictionary, the correct number of fields are returned but without a name (as the didSet does not fire). Any advise? Cheers - still loving the course. Paul
roygbiv
4 years ago
Hi Brian, Would like to check if my concepts are correct. In creating UserProfileController this is our custom class. When we want to execute a functionality - i.e. a log out button - we create a custom function and add it to viewDidload. However, for other types of functionality such as returning total number of cells in a collectionview, we don't put the function under viewDidLoad. This is because firstly, we are subclassing from UICollectionViewController and therefore the default total number of cells returned is 1. If we want to allow the returned cells to be dynamic, we must override the function. However we do not put the overridden function in viewDidload. Why is that ? Is it because that function belongs to UICollectionViewController instead of our custom class ?
Brian Voong
4 years ago
roygbiv
4 years ago
Ah yes.. that was the missing part. I understand now thanks Brian
Alex Grinberg
4 years ago
Hi Brian, can you help me with something? If the data that is returned from Firebase (I mean for example, if I fetch a list of items and there are none (empty tableView), how could I present a text "No data available" just in the case that the function for the firebase is empty?
Brian Voong
4 years ago
Alex Grinberg
4 years ago
Thanks Brian, it’s working now!
malrhex
4 years ago
Hi there, how do we actually click on this photos to make it bigger and be able to read the caption?
Brian Voong
4 years ago
malrhex
4 years ago
God you are fast to respond men! nice answer rapid as fire! Have a wonderful day!
malrhex
4 years ago
Hey men, in your course of $20 do you teach how to make a seach based on segmented control over "Device" and "cloud" ? cuz I need to filter certain of data using kind of the same methodology, lets say I want to search only post, and I want to search only users. The post will have a title. btw, I am very close to get the 70$ course too if you have something built with the like buttom for a match, lets say, you can play around with the like.
Hwang.the.human
4 years ago
Hello, Brian! My question is off-topic. I am using UUID().uuidString. Is that possible to get current random id?
Brian Voong
4 years ago
Vitor Ferraz
4 years ago
I would like to contribute with a solution to decode objects from firabase. Use this extension: import FirebaseDatabase extension DataSnapshot { func decoded<Type: Decodable>() throws -> Type { guard let dictionary = value as? [String: Any] else { fatalError("Error to decode value") } let jsonData = try JSONSerialization.data(withJSONObject: dictionary, options: []) let object = try JSONDecoder().decode(Type.self, from: jsonData) return object } func decodedList<Type: Decodable>() throws -> Type { var array = [[String: Any]]() guard let dictionaries = value as? [String: Any] else { fatalError("Error to decode value") } dictionaries.forEach({ (key, value) in if let dictionary = value as? [String: Any] { array.append(dictionary) } }) let jsonData = try JSONSerialization.data(withJSONObject: array, options: []) let object = try JSONDecoder().decode(Type.self, from: jsonData) return object } } and to use it: guard let posts: [Post] = try? snapshot.decodedList() else { return } guard let user: User = try? snapshot.decoded() else { return }
tsangaris
3 years ago
Hey Brian, What if the snapshot you retrieve from Firebase has a nested value (for example below a parent has children)? How would you tackle it? For example: struct Parent { let name: String let children: [Child] } struct Child { let name: String let gender: String let birthday: Int }
GoldenRules
3 years ago
Hi Brian, Thank you for all your great videos. I am currently building a social network app on Xcode and this is mostly thanks to your well made videos, I started with absolute zero knowledge in swift 4 years ago aside with my full-time job. I'm about to jump 100% on this app and releasing it on the App Store by September optimistically speaking. After that I will push heavily on the marketing side to encourage people to use it ( it is a social network to land your dream job way easily than now). My question is, for an hopefully large number of users, do you suggest to keep Firebase as a storage for all the data or is better to use an alternative way? I would like to purchase your course Fullstack Social iOS NodeJS REST but I just wanted to know what is the difference between the tools used there and in the Instagram Firebase. Thank you
Brian Voong
3 years ago
GoldenRules
3 years ago
Many thanks for the tip, I will have a look on your course to build a better groundwork ?
Andrew Thompson
2 years ago
Is this using the new Firestore or Real time database? Debating on purchasing, let me know, thanks!
Brian Voong
2 years ago
Andrew Thompson
2 years ago
Excellent I can’t wait for that one! I’ll purchase that! I have been watching your YouTube for two years now, thank you:)
HELP & SUPPORT