Firebase存储iOS在上传完成之前调用文件完成处理程序

时间:2018-03-02 06:49:46

标签: ios swift firebase

我尝试将视频上传到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控制台并下载了上传的文件,它还没有完成。

有人知道为什么吗?

1 个答案:

答案 0 :(得分:1)

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

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

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)") 
} 

它会给你一些类似的东西:

<NSProgress: 0x604000123020> :
Parent: 0x0 / Fraction completed: 0.0062 / Completed: 1867836 of
301329576

您还可以使用

获取有关失败的更多信息
 uploadTask.observe(.failure) { snapshot in
    if let error = snapshot.error as? NSError {
      print ("Error : \(error)")
    }
}

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