meetaks69
meetaks69

Reputation: 23

Imageurl not getting saved to Firestore database - SwiftUI

So I'm building a user profile in my app and saving Name, Bio, Image, uid, and date created in Firestore database. While other values are getting saved in the database and retrieved in user profile, the imageurl is getting saved as empty string, and hence not getting retrieved in the user profile. Where am I going wrong here?

import SwiftUI
import Firebase

class RegisterViewModel : ObservableObject{
    
    @Published var name = ""
    @Published var bio = ""
    
    @Published var image_Data = Data(count: 0)
    @Published var picker = false
    let ref = Firestore.firestore()
    
    // loading view
    @Published var isLoading = false
    @AppStorage("current_status") var status = false
    
    func register(){
        
        isLoading = true
        // setting user data to firestore
        let uid = Auth.auth().currentUser!.uid
        
        UploadImage(imageData: image_Data, path: "profile_Photos") { (url) in
            
            self.ref.collection("Users").document(uid).setData([
                
                "uid": uid,
                "imageurl": url,
                "username": self.name,
                "bio": self.bio,
                "dateCreated": Date()
            
            ]) {(err) in
                
                if err != nil{
                    self.isLoading = false
                    return
                }
                self.isLoading = false
                // success means setting status as true
                self.status = true
            }
            
        }
        
    }
    
}


import SwiftUI
import Firebase

func UploadImage(imageData: Data, path: String, completion: @escaping (String) -> ()){
    
    let storage = Storage.storage().reference()
    let uid = Auth.auth().currentUser!.uid
    
    storage.child(path).child(uid).putData(imageData, metadata: nil) { (_, err) in
        
        if err != nil {
            
            completion("")
            return
            
        }
        
        // downloading url and sending back
        
        storage.child(path).child(uid).downloadURL { (url, err) in
            
            if err != nil {
                
                completion("")
                return
                
            }
            
            completion("\(url!)")
            
        }
        
    }
    
}

The result in Firestore database is something like:

bio: "Bio"

dateCreated: September 22, 2022 at 1:32:43 AM UTC+5:30

imageurl: ""

uid: "o32JbCwShtKLhtHibcui474NwFA9"

username: "Name"

Upvotes: 0

Views: 79

Answers (1)

slushy
slushy

Reputation: 12385

Swift's URL is not a supported type in Firestore. You must convert the URL to a supported type, like String, before saving it.

"imageurl": url!.absoluteString

You must then reconstruct the URL when you fetch the string value from Firestore.

let imageurl = URL(string: imageurl)

Upvotes: 1

Related Questions