I want to fetch an Image Url from my firebase storage and assign it to the User’s profileImageUrl variable but it seems like I can not directly assign it in the function. Would there be a way to set something like profileImageUrl = url?.absoluteString?
import Foundation
import FirebaseFirestoreSwift
import FirebaseStorage
struct User: Codable, Identifiable, Hashable{
@DocumentID var uid: String?
let fullname: String
let email: String
var profileImageUrl: String?
var id: String {
return uid ?? NSUUID().uuidString
}
var firstName: String {
let formatter = PersonNameComponentsFormatter()
let components = formatter.personNameComponents(from: fullname)
return components?.givenName ?? fullname
}
func fetchProfileImageUrl(completion: @escaping (String?) -> Void) {
guard let uid = uid else {
completion(nil)
return
}
let storageRef = Storage.storage().reference().child("profile_images/(uid).jpg")
storageRef.downloadURL { url, error in
if let error = error {
print("Error fetching profile image URL: (error)")
completion(nil)
} else {
completion(url?.absoluteString)
}
}
}
}
I’ve tried creating a separate model for the user but still the the profileImageUrl is always nil.
Yurock Heo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.