0

I have a working function that downloads data from my Firebase Storage, but I don't understand how to actually use the data in my project.

func getImg2() {
        let storageRef = storage.reference()
        let imageRef = storageRef.child("images/hi2.png")
        
        _ = imageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                print(error.localizedDescription)
            } else {
                let image = UIImage(data: data!)
                
                print("Data: \(data!)")
                print("succesfully downloaded \(image!)")
            }
        }
    }

The data gets printed, as well as the image information. I would like to return this data and use it outside the function to load a UIImage. Everything I've tried has given me an error or just returned "0 bytes" for the data. I've been stuck on this for days and would greatly appreciate any help.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
phast
  • 188
  • 1
  • 2
  • 12
  • 1
    Look up "completion handler" or "callback function" – jnpdx Feb 11 '22 at 16:58
  • @Frank van Puffelen can you please re-open my question? The post you suggested is not the same and does not answer my question. – phast Feb 11 '22 at 20:14
  • You can't return something *now* that is asynchronously loaded, as it won't be available until *later*. You will need to pass in a callback function, that you call once the data is available. The questions I've linked show various ways to do that. --- If your problem is different than that, can you show us how it's different? For example, did you try implementing a callback function and did that not work? Can you show that? – Frank van Puffelen Feb 11 '22 at 23:07

0 Answers0