When my user signs up, they are using a profile image that goes directly to firebase. The image uploads into firebase storage the properly but once it appears in the database the profile image URL is missing also in the debug area the image link is showing up as optional. What should I change in this code to properly upload the image link into the firebase database?
if let AuthData = AuthDataResult {
print(AuthData.user.email)
var dict : Dictionary < String, Any> = [
"uid": AuthData.user.uid,
"username": self.UsernameTextField.text!,
"email": AuthData.user.email,
"ProfileImageUrl": "",
]
let storage = Storage.storage()
let storageRef = storage.reference()
let imageName = UUID().uuidString
let imageReference = Storage.storage().reference().child(imageName)
//2. Compress quality
if let uploadData = self.ProfileImage.image?.jpegData(compressionQuality: 0.5){
//3. Save image as .jpeg
let metaDataForImage = StorageMetadata()
metaDataForImage.contentType = "image/jpeg"
//4. Add the data to Firebase Storage
imageReference.putData(uploadData, metadata: metaDataForImage) { (meta, err) in
if let err = err{
print(err.localizedDescription)
} else {
//5. Retrieving the image URL
imageReference.downloadURL { (url, err) in
if let err = err{
print(err.localizedDescription)
} else {
//6. Print the complete URL as string
let urlString = url?.absoluteString
print(urlString)
}
Database.database().reference().child("users").child(AuthData.user.uid).updateChildValues(dict, withCompletionBlock: { (error,ref) in
if error == nil {
print("Done")
return
}