简体   繁体   中英

Can I get progress while uploading/downloading data from Firebase

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM