init(type: String, color: String, brand: String, price: String, image: UIImage) {
    self.type = type
    self.color = color
    self.brand = brand
    self.price = price
    self.imageURL = setImage(image: image)
}
func setImage(image: UIImage?) -> String {
    if image != nil {
        let unwrappedImage: UIImage = image!
        let image = unwrappedImage.pngData()!
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let data = image
        var iURL: String = "images/\(String(describing: UUID())).jpg"
        let dataRef = storageRef.child(iURL)
        _ = dataRef.putData(data, metadata: nil) { (metadata, error) in
            guard let metadata = metadata else {
                print("error uploading image")
                return
            }
            _ = metadata.size
            dataRef.downloadURL { (url, error) in
                if error != nil {
                    print("error uploading image")
                } else {
                    print(url!.absoluteString)
                    iURL = url!.absoluteString
                }
            }
        }
        return iURL
    }
    return ""
}
I am trying to take an image upload it to Firebase storage and save the absolute URL to a field in the struct. After the struct is created, I upload it to a Firebase database. My issue is that at the moment the struct is created and uploaded with the old value of iURL, before the async call goes through with the absolute url.
I tried to use a dispatch queue to fix this:
let serialQueue = DispatchQueue(label: "imageupload.serial.queue")
        serialQueue.sync {
            _ = dataRef.putData(data, metadata: nil) { (metadata, error) in
                guard let metadata = metadata else {
                    print("error uploading image")
                    return
                }
                _ = metadata.size
                dataRef.downloadURL { (url, error) in
                    if error != nil {
                        print("error uploading image")
                    } else {
                        print(url!.absoluteString) //(1)
                        iURL = url!.absoluteString
                    }
                }
            }
        }
        serialQueue.sync {
            
            self.imageURL = iURL //(2)
            return
        }
But it made no difference (2) still happened before (1)
 
     
    