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
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))")
})
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();