MathEnthusiast
MathEnthusiast

Reputation: 101

Firebase putData function

I am using Xcode, Swift 3. I am trying to call putData for an image (PNG), and this is my code :

let storageRef = Storage.storage().reference().child("ProductsImages").child(product.UniqueID()).child("MainImage.png")
    if let mainChosenImage = self.selectedImageToUpload
    {
        if let uploadData = UIImagePNGRepresentation(mainChosenImage)
        {
            storageRef.putData(uploadData, metadata: nil, completion:
                {
                    (StorageMetaData, error) in
                    if error != nil
                    {
                        print(error)
                        return
                    }

                    self.mainImageURL = StorageMetaData?.downloadURL()?.absoluteString
            })
        }
    }

And the behavior is this - the image is saved fine to Firebase, but "completion" isn't called. Meaning - it won't check if error != nil, and more importantly - it won't instantiate mainImageURL with the absolute URL string.

Any ideas on how to fix this ?

Upvotes: 0

Views: 1691

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100541

completion should look like this

 if let uploadData = UIImagePNGRepresentation(self.myImageView.image!) {
    storageRef.put(uploadData, metadata: nil) { (metadata, error) in
        if error != nil {
            print("error")

        } else {

            // your uploaded photo url.
        }
   }

Upvotes: 1

Related Questions