'Firebase Database user's profileImageUrl not updating

I want to update firestore databases property profileImageUrl with my uploaded pictures storage url ( storage code works fine, I can see the uploaded image with its own URL ) any ideas ?

if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
    myImage = image
    self.mainProfilePicImage.image = image
} // where I choose image

guard let imageData = self.mainProfilePicImage.image?.jpegData(compressionQuality: 0.4) else {
    return
} // compressing to jpeg

let userID = Auth.auth().currentUser!.uid
let storageRef = Storage.storage().reference(forURL: "gs://xxxxxxx.appspot.com/") // my firebase storage
let storageProfileRef = storageRef.child("profile").child(userID)

let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
storageProfileRef.putData(imageData,metadata: metadata) { (storageMetadata, error) in
    if error != nil {
    print("error")
    }
    storageProfileRef.downloadURL(completion: {(url,error) in
        if let metaImageUrl = url?.absoluteString { // downloading url from storage of picked image 
            print("meta image url: \(metaImageUrl)") // printing URL
            let db = Firestore.firestore()
            db.collection("users").document(userID).updateData([
                "profileImageUrl": metaImageUrl,
            ]) { err in
                if let err = err {
                    print("Error updating document: \(err)")
                } else {
                    print("Document successfully updated")
                }
            }
        }
    })
}

firestore configuration for better understanding



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source