Delaying A Label Updated From Within connection:didReceiveData:data
-
01-06-2021 - |
Question
I'm working on a download manager and I'm trying to update labels indicating the download speed and the percentage left.
It updates way too fast within connection:didReceiveData though, and using a performSelector and delaying it from that method doesn't work (it just delays it for that long before starting).
What should I do to update something like download speed or percentage left?
Solution
If you don't want to update the display as quickly as possible, just keep note of the time when you last updated it. If the current time is not at least, say, 0.5 seconds since the last update, just don't update on this pass.
OTHER TIPS
It would be helpful to see how you calculate speed and percentage but one approach will be :
- Calculate time elapsed (in seconds) between two calls of
connection:didReceiveData
- Take the size of the current received data (let's call it
size
) - Sum total data size downloaded so far (lets call it
downloadedSize
) Speed = size / timeElapsed
Percentage = downloadedSize / Total data size
(Assuming you have it)- Convert to desired units
- Update the labels (I do not see why you should delay here)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow