我正在使用以下代码将一个故事的多个音频文件上传到Firebase存储中:
func uploadAudio(storyforUpload:
Story, indexPath: IndexPath) {
//set Firebase storage reference
let uploadAudioRef = Storage.storage().reference().child("audiofiles")
let storyItems =
storyforUpload.storyItems
let totalItems = storyItems.count
var itemCount = 0
//Store audio to Fire Storage
for item in storyItems {
//Copy the audiofiles from local storage to firebase - This works no problem
let taskRef = audioItemRef.putFile(from: localAudioFile, metadata: uploadAudioMetaData) { (downloadMetaData, err3) in
if let err3 = err3 {
print("Error for audio upload is \(err3)")
return
}
// print("audio upload successful")
}
taskRef.observe(.progress) { (snapshot) in
let pctThere = snapshot.progress!.fractionCompleted
print("You are \(pctThere) complete")
}
taskRef.observe(.success) { (snapshot1) in
print("Audio uploaded success \(snapshot1)")
itemCount = itemCount + 1
let status = itemCount / totalItems
print("status is \(status)")
}
}
}
为了跟踪多文件上传的状态,我创建了“ itemCount”和“ totalItems”,并打算将其更改为百分比(“状态”),以显示在viewController单元格中的标签中。但是,即使itemCount递增且totalItems设置为预期值,状态(即itemCount / totalItems)也不会计算。如果打印值是itemCount为'5',totalItems为'9',状态为'0',则典型输出。实际上,状态仅在到达最后一项时才显示“ 1”。
我已经尝试了几种方法来解决此问题:
全部无济于事。如果有人可以告诉我我要去哪里错了,请多谢。谢谢!
答案 0 :(得分:0)
我刚刚意识到自己的错误。 “ itemCount”和“ totalItems”都是整数!因此结果将仅为整数。将它们更改为两倍可以解决此问题...我现在拍了拍自己的头。