I'm trying to create this function using Swift on Xcode 8. And after I've got the url using the firebase function, I cannot assign the url
value to imageURL
.
I've tried a workaround - putting the imageURL outside of the function, and assign the url
function to self.imageURL
and it works perfectly. However, I'm trying to make this a static function, so I cannot use that workaround. Does anyone know how to solve this?
func createImageDownloadURL(path: String) -> URL? {
// Create a reference to the file you want to download
let storage = FIRStorage.storage()
let storageRef = storage.reference()
let imageRef = storageRef.child(path)
var imageURL: URL?
// Fetch the download URL
imageRef.downloadURL { (url: URL?, error: Error?) in
if error != nil {
// Handle any errors
print(error!)
} else {
// Get the download URL for image
DispatchQueue.main.async {
// Run UI Updates
print(imageURL) // output looks perfectly fine
imageURL = URL(string: url!.absoluteString) // This is where the problem is
}
}
}
print(imageURL) // output is "nil"
return imageURL
}