Alby
Alby

Reputation: 273

how to track a single download speed with asihttprequest

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

Upvotes: 6

Views: 1900

Answers (2)

skwashua
skwashua

Reputation: 1627

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];
}

Upvotes: 3

JosephH
JosephH

Reputation: 37495

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

Upvotes: 2

Related Questions