0
votes

I'm experiencing a weird issue when I try to upload an image to Firebase Storage. Essentially I am able to upload the file to Storage, but the completion callback is not happening. I need this callback to get the Storage URL of the file being uploaded.

SETUP

let storage = Storage.storage()
let storageRef = storage.reference()
let data = curImg?.pngData()
let riversRef = storageRef.child("userUploads/Search/"+id+".png")

UPLOAD FUNCTION

let uploadTask = riversRef.putData(data!, metadata: nil) { (metadata, error) in
    print("FIN") // NEVER PRINTS
    print("DONE",metadata,error) // NEVER PRINTS
}

This doesn't ever print anything

USING OBSERVE

uploadTask.observe(.success) { snapshot in
    // Upload completed successfully
    print("SUCCESS") // NEVER PRINTS
    riversRef.downloadURL(completion: { (url, error) in
        print("A") // NEVER PRINTS
        print(url,error) // NEVER PRINTS
    })
}

This doesn't ever print anything

A BIT MORE HACKY

uploadTask.observe(.progress) { snapshot in
    print(snapshot) // PRINTS!
    if (Double(snapshot.progress!.completedUnitCount) == Double(snapshot.progress!.totalUnitCount)) {
        print("HERE") // PRINTS!!
        riversRef.downloadURL(completion: { (url, error) in
            print("B") // NEVER PRINTS
            print(url,error) // NEVER PRINTS
        })
    }
}

Is able to print progress and I am able to figure out when it finally completes, but I can never get the downloadURL to complete :(

EDIT I tried updating pods and installing all again and didn't help.

1
A few suspects. let data = curImg?.pngData() is odd. 'curImg' not included in the question. If that's a UIImage, it's shouldn't be optional so the ? is suspicious. But then we have let riversRef = storageRef.child("userUploads/Search/"+id+".png") but id is not included so we don't know what that is. Lastly, the '.putData(data!' is unwrapping an optional, and if it happens to be nil, the app will crash so that's not a good idea. Can you update the question with a Minimal, Complete, and Verifiable exampleJay

1 Answers

0
votes

If you encounter this weird issue, there's nothing wrong with the code. I had to roll back all my pods by deleting Podfile.lock using pod 'FirebaseInstanceID', '2.0.0'. Weird bug in the latest versions of Firebase / Xcode...?