[英]Swift - Problem While Downloading Data From Firebase Firestore Asynchronously with DispatchGroup()
[英]Can I get progress while uploading/downloading data from Firebase
我需要在从 Firebase 数据库上传/下载数据时显示进度。 我能得到它吗?。 我在 Firebase 存储中看到,但在 Firebase 数据库中没有看到。
我建议您查看这篇文章,因为它将帮助您了解如何使用一些内置数据参考来确定您的上传进度。 虽然它不适用于 iOS,但它解释了如何衡量上传进度的思考过程。
您始终可以使用这样的进度监视器:
let observer = uploadTask.observeStatus(.Progress) { snapshot in
print(snapshot.progress) // NSProgress object
}
至于FrankvanPuffelen所说的,实际上没有任何工具可以满足您的要求。 然而,随着周杰伦的状态,可以判断基于你是如何读/写你的任务(一个或多个)的“进步”。
例如,假设您正在编写(上传)10 张照片。 对于成功写入(上传)的每张照片,您可以简单地将某种进度表增加 1/10。
用户设备上某些本地文件的示例:
// Some image on the device
let localFile = URL(string: "<PATH>")!
// Make a reference to the file that needs to be uploaded
let riversRef = storageRef.child("chicken.jpg")
// Upload the file to the path you specified
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
if let error = error {
// Whoops, something went wrong :(
}
else {
// Tada! Uploaded file
someCountVariable += 1
// If needed / applicable
// let downloadURL = metadata!.downloadURL()
}
}
当然,对于一 (1) 个项目,您将从 0 到 100(.. 真的很快 - 向 Drake 大声喊叫),但这可以嵌套在某个循环中,该循环遍历要上传的一些项目列表。 然后每个项目成功上传增量。
如果您需要上传多个图像以外的对象,这将在请求方面更加昂贵,但为此目的,这将为您提供一种很好的视觉/数字方式来跟踪至少还有多少项目可以成功上传。
如果您真的想深入了解细节,您可以随时尝试检查当前连接强度以确定相对数据传输速度,并将其与您正在尝试的当前任务交叉。 这可能会根据流程应该花费的时间到实际花费的时间,至少为您提供一些估计的进度。
希望其中的一些帮助/为您指明正确的方向。 快乐编码!
val bytesTransferred=taskSnapshot.bytesTransferred.toFloat()
val totalByteCount=taskSnapshot.totalByteCount.toFloat()
val progress = bytesTransferred / totalByteCount
Log.d("progress", (progress*100).toString())
Log.d("progressDivide", (bytesTransferred / totalByteCount).toString())
Log.d("progressbytesTransferred", bytesTransferred.toString())
Log.d("progresstotalByteCount", totalByteCount.toString())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.