从Firebase上传/下载数据时,我可以取得进展吗? [英] Can I get progress while uploading/downloading data from Firebase

查看:171
本文介绍了从Firebase上传/下载数据时,我可以取得进展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Firebase数据库上传/下载数据时显示进度。
我可以得到它吗?我在Firebase存储中看到了,但是在Firebase数据库中没有看到。

解决方案

基于数据的进度:



我推荐查看这个帖子 ,因为它将帮助您了解如何使用一些内置的数据引用来确定您的上传进度。对于iOS而言,它不是 ,它解释了如何衡量上传进度的思考过程。

您可以随时使用进度监视器例如:
$ b $ pre $ let observer = uploadTask.observeStatus(.Progress){快照在
print(snapshot.progress)/ / NSProgress对象
}



基于计数的进度:

至于 FrankvanPuffelen 的说法,实际上没有工具可以给你你在问什么但是,正如 Jay 所述,您可以确定您的任务的进度基于如何你正在阅读/写作。比如说你正在写(上传)10张照片。对于成功书写(上传)的每张照片,您只需将某种进度表增加1/10即可。



用户设备上的一些本地文件的示例:

  / /设备上的一些图像
let localFile = URL(string:< PATH>)!

//引用需要上传的文件
let riversRef = storageRef.child(chicken.jpg)

//上传文件到您指定的路径
let uploadTask = riversRef.putFile(from:localFile,metadata:nil){元数据,错误
如果发生错误=错误{
//哎呀,错误:(

其他{
// Tada!上传文件
someCountVariable + = 1

//如果需要/适用
//让downloadURL =元数据!.downloadURL()
}
}

当然,对于一个物品,你将会从0到100(..real快速 - 向Drake大喊),但是这可以嵌套在一些循环中,循环遍历一些需要上传的物品列表,然后每个物品成功上传增量。

如果您需要上传多个图像以外的对象,请求方面会更加昂贵,但为此目的,你有一个很好的视觉/数字的方式来跟踪至少有多少项目是成功上传。



如果你 <如果你真的想做到这一点,你总是可以尝试检查当前的连接强度,以确定一个相对的数据传输速度,并与当前正在尝试的任务交叉。这可能会使您至少 根据流程 应该 花费多长时间来估计进度。



希望这有助于您/朝正确的方向。快乐的编码!


I need to show progress while uploading/downloading data from Firebase database. Can I get it?. I saw in Firebase storage but I didn't saw in Firebase database.

解决方案

Progress based on data:

I recommend looking over this post, as it will help you understand how to use some of the built in data references to determine your upload progress. While it is not for iOS, it explains the thought process of how you can measure upload progress.

You can always use a progress monitor like such:

let observer = uploadTask.observeStatus(.Progress) { snapshot in
  print(snapshot.progress) // NSProgress object
}

Progress based on count:

As to what FrankvanPuffelen said, there is in fact no tool to give you what you are asking for. However, as Jay states, you can determine the "progress" of your task(s) based on how you are reading/writing.

Say for instance you are writing (uploading) 10 photos. For each photo that is successfully written (uploaded), you can simply increment some sort of progress meter by 1/10.

Example for some local file on the users device:

// 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()
    }
}

Of course for one (1) item you will go from 0 to 100 (..real quick - shout out to Drake), but this can just be nested in some loop that iterates through some list of items to upload. Then per each item successfully uploaded increment.

This will be more expensive in terms of requests if you need to upload objects other than multiple images, but for the purpose this will give you a nice visual / numeric way of tracking at least how many items are left to be successfully uploaded.

If you really want to get down to the nitty gritty, you can always try checking the current connection strength to determine a relative data transfer speed, and cross that with the current task you are attempting. This could potentially give you at least some estimate the progress based on how long the process should take to how long it actually has taken.

Hope some of this helps / points you in the right direction. Happy coding!

这篇关于从Firebase上传/下载数据时,我可以取得进展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆