【问题标题】:Firebase Storage putData method never executes completion blockFirebase Storage putData 方法从不执行完成块
【发布时间】:2019-07-07 08:23:03
【问题描述】:

我正在尝试将图像上传到 Firebase 存储,然后将用户配置文件保存到数据库中。

但似乎未执行 storageRef.putData 方法->从未打印过“输入数据闭包”。 (打印“uploadProfileImage called”。)

这是在 Auth.auth().createUser 闭包中调用的函数。

func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    print("uploadProfileImage called")
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        print("In put data closure")
        if error == nil, metaData != nil {
            storageRef.downloadURL(completion: { (url, error) in
                if let error = error {
                    print("url error")
                } else {
                    completion(url?.absoluteURL)
                }
            })
        }
    }
}

但我可以在 firebase 存储中看到该图像,因此已上传。为了保存个人资料,我需要这张图片的网址。

感谢您的帮助!

【问题讨论】:

  • putData 应该返回上传任务。你试过用observeStatus观察它的状态,看看有没有错误?
  • 嘿,我试过 storageRef.putData(imageData).obsserver(.progress) { (snapshot) in .. 并且 snapshot.error 总是 nil 并且 snapshot.progress 是:“可选( : Parent: 0x0 / Fraction completed: 1.0000 / Completed: 19544 of 19544)"

标签: ios swift firebase firebase-storage


【解决方案1】:

只需在 podfile 中将“FirebaseInstanceID”修复为“2.0.0”版本,putData 方法就会被执行。在 podfile 中看起来像这样: pod 'FirebaseInstanceID', '2.0.0'

我正在使用 Xcode 10.2 和 Swift 5,这个技巧仍然有效

【讨论】:

    猜你喜欢
    • 2017-11-27
    • 2020-01-27
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多