update progress bar with Firebase uploading

后端 未结 2 891
有刺的猬
有刺的猬 2020-12-08 16:36

I tried to add a progress bar to uploading files on firebase. but unfortunalty it does not indicate upload progress. both logcat & progress bar only indicate when file

相关标签:
2条回答
  • 2020-12-08 17:09

    When you are sending data up to storage you can observe the progress of what you are uploading.

    let uploadTask = profileRef.putData(data, metadata: metadata) { (metadata, error) in
         if let error = error {
                  // Awesome error handling 
         }
    }
    

    Now that you have started sending data up observe the changes. The snapshot you get back has a property on it called fractionCompleted. You can set a progress bar with min and max value of 0 and 1 and set the value to the fraction completed value. Or if you prefer just show the percentage of the upload as it comes back.

    task.observe(.progress, handler: { (snap) in
       print("Our upload progress is: \(String(describing: snap.progress?.fractionCompleted))")
    })
    
    0 讨论(0)
  • 2020-12-08 17:21

    Change the grouping of terms in your calculation of progress to force conversion to float. As your code is now, you are dividing two longs. The result of the division will be 0 until getBytesTransferred() == getTotalByteCount(), then it will be 1.

     double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
    
    0 讨论(0)
提交回复
热议问题