Why .putData does not upload Image in Firebase Storage with Swift?

1 answer

I have a simple swift App in which the user chooses an image from the Gallery, in addition to entering a few data. By clicking on Save the image chosen should be uploaded into Firestore Storage. I implemented the same logic described here https://firebase.google.com/docs/storage/ios/upload-files. When I call the ".putData(data, metadata: nil)" nothing happens and no error is given back. Here is the code snippet:

    func uploadImage(imageView: UIImageView) -> String {          // Variable for my image url         var urlString: String         urlString = ""          storage = Storage.storage()          guard let image = imageView.image,             let imageData = image.jpegData(compressionQuality: 1.0) else {                 print ("no image uploaded")                 return ""         }         let imageName = String (Int64((NSDate().timeIntervalSince1970 * 1000.0).rounded())) + ".jpg"         let imageRef = storage.reference().child(Constants.IMAGES_PATH).child(imageName)          // QUOTE : THIS PART DOES NOT GIVE ANY FEEDBACK. NONE OF THE PRINTS INSIDE IT IS EXECUTED         let uploadTask = imageRef.putData(imageData, metadata: nil) { (metadata, error) in           if let error = error {             // Uh-oh, an error occurred!             print(error)             return           }           imageRef.downloadURL { (url, error) in             guard let downloadURL = url else {               print ("error")               return             }             urlString = downloadURL.absoluteString             print("Downloaded image URL ", urlString)           }         }         // UNQUOTE          print ("after uploadTask", imageRef.downloadURL) // THE URL IS ALWAYS EMPTY ""          return urlString      }  

I set a debugger breakpoint, which shows that the code inside the uploadTask is not executed, just the uploadTask is executed and everything inside is skipped.

The uploadImage function is called after picking the Image like this:

        var image = uploadImage(imageView: imageView)  

The image is picked here (this works fine):

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {         if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {             imageView.contentMode = .scaleAspectFit             imageView.image = pickedImage             print("image chosen")         }          dismiss(animated: true, completion: nil)     }      func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {         dismiss(animated: true, completion: nil)     }  

I initialised like this, which should be ok:

import UIKit import os.log import FirebaseFirestore import Firebase  class DisplayMediumViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {      var storage: Storage!      override func viewDidLoad() {         super.viewDidLoad()          // Additional setup after loading the view.         imagePicker.delegate = self          // Create tap gesture recognizer         let tapGesture = UITapGestureRecognizer(target: self, action: #selector(DisplayMediumViewController.imageTapped(gesture:)))          // Aadd it to the image view;         imageView.addGestureRecognizer(tapGesture)          // Make sure imageView can be interacted with by user         imageView.isUserInteractionEnabled = true       }   

I searched a lot in the internet and tried different ways to implement it, none was successful. It would be great, if someone has a hint why the "uploadTask = imageRef.putData" does not upload the image nor return any downloadURL.

Received answer to this question:
The best answer according to the author of the question:

putData is an asynchronous call with a callback function. The print and return are executed before putData has a chance to return.

If you put a breakpoint at if let error = error you should be able to see the results.

See similar question/answer at https://stackoverflow.com/a/57639986/556617