【问题标题】:Firebase Storage iOS put file completion handler called before upload finishesFirebase Storage iOS 在上传完成之前调用文件完成处理程序
【发布时间】:2018-03-02 06:49:45
【问题描述】:

我试图将视频上传到 Firebase 存储,上传完成后,我将此视频文件的位置存储到数据库中的一个对象中,我的代码如下:

Storage.storage().reference(withPath: mediaURL!).putFile(from: localCacheURL, metadata: nil, completion: { (metadata, error) in
    if error != nil {
        print("❗️failed to upload video")
    } else {
        print("a video is uploaded")
        json = ["text": "", "image": "", "video": mediaURL!, "connections": []]
        upload()
    }
})

但是我发现在上传实际完成之前调用了完成处理程序,我去了 Firebase 控制台并下载了上传的文件,它没有完成。

有人知道为什么吗?

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:

    您的问题的一个原因可能是您的缓存文件以某种方式损坏。要弄清楚发生了什么,您需要更多信息。

    您可以通过观察由putFile返回的FIRFileStorageUploadTask来做到这一点。

    let uploadTask = Storage.storage().reference(withPath: mediaURL!).putFile(from: localCacheURL, metadata: nil, completion: { (metadata, error) in
        if error != nil {
            print("❗️failed to upload video")
        } else {
            print("a video is uploaded")
            json = ["text": "", "image": "", "video": mediaURL!, "connections": []]
            // unclear what this does 
            // looks strange if it's just a notification rename it into something like videoUploadCompleted 
            // if it needs the json stuff pass it as parameter to make it thread safe
            upload() 
        }
    })
    
    uploadTask.observe(.progress) { snapshot in
       print("\(snapshot.progress)") 
    } 
    

    它会给你类似的东西:

    : 父级:0x0 / 部分已完成:0.0062 / 已完成:1867836 301329576

    您还可以通过使用获取有关失败的更多信息

     uploadTask.observe(.failure) { snapshot in
        if let error = snapshot.error as? NSError {
          print ("Error : \(error)")
        }
    }
    

    有关监控和错误处理的更多详细信息,另请参阅 Upload Files on iOS Firebase 文档

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2018-10-23
      • 2014-08-16
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多