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

问题描述 投票:0回答:1

我试图将视频上传到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个回答
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文档

© www.soinside.com 2019 - 2024. All rights reserved.