I am downloading some data from the server in my iOS App, now I want to know the speed of downloading the data, becoz I want to stop downloading if it take more than 10 min in downloading, I am using ASIHTTPRequest for downloading. If downloading process is taking more than 10 mins then I want to give a message to the user that if he/she wants than can stop the process. Plz suggest me any solution.
2 Answers
You can already time how many bytes were downloaded, by using a simple timer. (clock() or whatever it is you are using)...
That gives you the sliding rate at which the data is being downloaded ... difference in bytes/difference in time..
Now, use the remaining bytes to be downloaded, and estimated time = remaining bytes/rate calculated above.
if estimated time > 10minutes, then stop. (close the socket or something)

- 1,786
- 11
- 13
-
http://stackoverflow.com/questions/3519562/how-do-i-write-a-timer-in-objective-c <-- see here for how to implement a timer with the Date class... – Karthik Kumar Viswanathan Jun 11 '12 at 13:10
-
`Timer *timer = [[Timer alloc] init]; [timer startTimer]; cur_bytes_read = read_bytes(); [timer stopTimer]; cur_time = [timer timeElapsedInSeconds]; cur_rate=cur_bytes_read/cur_time; tot_bytes_read+=cur_bytes_read; time_remaining = (tot_bytes - tot_bytes_read) / cur_rate; if(time_remaining > 600) { stop_reading_bytes(); }` – Karthik Kumar Viswanathan Jun 11 '12 at 13:12
@Karthik Kumar Viswanathan's solution should work, but I wanted to add that if you are going to display the download speed to your users, you should probably only use the past couple seconds of data transferred when calculating the speed. Otherwise you will run into issues with "average" speeds; e.g. if the connection downloads at 50 kbps for 10 seconds, then at 0 kbps for 10 seconds, it will show that it's downloading at 25 kbps at the end of those 20 seconds, when we know that it hasn't downloaded any data for 10 seconds.
Obviously if you are calculating this just for your own use, then the averaged speed would be more appropriate.

- 12,937
- 5
- 50
- 82