iOS development download file speed calculation

Source: Internet
Author: User

When we write the download interface, we need to show the user how many kb are downloaded per second, this time we need to calculate the speed. As follows:

I use afnetworking to do the download, we take afhttprequestoperation to raise the column, Afhttprequestoperation has a method:

[HTML] view plain copy

  1. -(void) Setdownloadprogressblock: (void (^) (Nsuinteger bytesread, Long long totalbytesread, long long Totalbytesexpectedtoread)) block

This method reads the guide data and then passes the block callback. The calculation speed is done in this block.

You need to define a download object: Downtask. Downtask Required Attributes: Totalread (Data read in one second), speed (velocity), date (the time after the last second calculation). Note: Here one second is not a strict sense of a second, it may be more than a second, we calculate the average speed, so no error is too large.

[HTML]View Plain Copy
  1. Calculate the speed in one second
  2. Downtask.totalread + = Bytesread;
  3. Get current time
  4. NSDate *currentdate = [NSDate Date];
  5. The current time and the previous second time to compare, greater than or equal to one second to calculate
  6. if ([currentdate timeIntervalSinceDate:downTask.date] >= 1) {
  7. Time difference
  8. Double Time = [currentdate timeIntervalSinceDate:downTask.date];
  9. Calculation speed
  10. Long long speed = downtask.total/time;
  11. Turn the speed into KB or m
  12. Downtask.speed = [Downtask formatbytecount:speed];
  13. Maintain variables, calculate cleared zeros
  14. Downtask.totalread = 0.0;
  15. Maintain variables and record the time of this calculation
  16. Downtask.date = currentdate;

wherein, downtask.speed = [Downtask formatbytecount:speed]; is used to convert bytes into the KB or m we need. The specific implementation is as follows:

[HTML] view plain copy

  1. -(nsstring*) Formatbytecount: (Long long) size
  2. {
  3. return [Nsbytecountformatter stringfrombytecount:size countstyle:nsbytecountformattercountstylefile];
  4. }


This method is placed in the downtask of the downloaded object.

iOS development download file speed calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.