Ad

Metadata?.storageReference?.downloadURL() Returns Nil

I'm trying to download an url to store in my database but the new update in Firebase prohibits me from doing this by requiring me to use the new way to download urls. Here's my code:

 let imageName = NSUUID().uuidString
 let storageRef = storage.reference().child("project_images").child("\(imageName)")

    guard let uplodaData = UIImageJPEGRepresentation(image, 1) else {
        return
    }


    let uploadTask = storageRef.putData(uplodaData, metadata: nil, completion: { (metadata, error) in

        if error != nil{
            print(error ?? "Failed to upload data in the uploadImages object there was an error:", error!)
            return
        }

If I put a breakpoint here it doesn't continue past the next section, I don't know why. I normally see these problems when storing in the wrong node or something.

metadata?.storageReference?.downloadURL(completion: { (url, error) in


        if error != nil {
            print("Failed to download url:", error!)
            return
        }

        let url = "\(String(describing: url))"
        uploadedImageUrlsArray.append(url)

        uploadCount += 1
        if uploadCount == imagesCount{
            completionHandler(uploadedImageUrlsArray)
        }
    })
})

When I but a breakpoint I can inspect the error variable in the completion and it's nil.

Could someone please tell me what I'm doing wrong here.

Ad

Answer

You created a storage reference for this specific image, so use that instead of metadata?.storageReference.

storageRef.downloadURL(completion: { (url, error) in


    if error != nil {
        print("Failed to download url:", error!)
        return
    }

    let url = "\(String(describing: url))"
    uploadedImageUrlsArray.append(url)

    uploadCount += 1
    if uploadCount == imagesCount{
        completionHandler(uploadedImageUrlsArray)
    }
  })
})
Ad
source: stackoverflow.com
Ad