Reputation: 77
I'm using firebase with swift. Below is my code to upload an image from an image picker, and to store the download url of the image. the point of my code is to store the download url after uploading the image. So I'm trying to find a way to wait for the upload process to finish in order to proceed.
_ = imageRef.putData(data, metadata: nil, completion: {(metadata,error) in
guard let metadata = metadata
else{
print(error)
return
}
})
imageRef.downloadURL { (URL, error) -> Void in
if (error != nil) {
// Handle any errors
} else {
// Get the download URL for 'images/stars.jpg'
let UrlString = URL?.absoluteString}
Upvotes: 0
Views: 905
Reputation: 317487
That's what the completion handler is for. You're already passing a completion handler to putData, which will be invoked when the upload is finished. You should check the error object to make sure it completed successfully.
This is documented, along with a code sample. You can see in the sample that the download URL is fetched from inside the completion handler, only if there is no error.
Upvotes: 1