6

Can someone help me with asihttprequest ?

I want to track the speed download of each file I download and not the average speed of all the files.

For the average spped of all downloads, there is [ASIHTTPRequest averageBandwidthUsedPerSecond] but what can I use to track each downloads?

Thanks

Alby
  • 273
  • 6
  • 14

2 Answers2

3

Considering ASI is dead, I recently had to do this on an older project. If somebody else needs help:

-(void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
    if (!lastBytesReceived)
        lastBytesReceived = [NSDate date];

    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:lastBytesReceived];

    float KB = (bytes / 1024);

    float kbPerSec =  KB * (1.0/interval); //KB * (1 second / interval (less than one second))

    NSLog(@"%llu bytes received in %f seconds @ %0.01fKB/s",bytes,interval, kbPerSec);

    lastBytesReceived = [NSDate date];
}
skwashua
  • 1,627
  • 17
  • 14
2

You can set a downloadProgressDelegate for each request, which will get a request:didReceiveBytes: call every time some data is received - you can use that to calculate the download speed.

See here in the documentation:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#custom_progress_tracking

JosephH
  • 37,173
  • 19
  • 130
  • 154
  • How can I calculate the total time elapsed from the first received byte ? – Alby Jun 12 '11 at 20:32
  • You can you store the result of [NSDate date] in the didReceiveResponseHeaders delegate method, then in didReceiveBytes use [NSData timeIntervalSinceReferenceDate:] – JosephH Jun 13 '11 at 08:27