how to track a single download speed with asihttprequest
Can someone help me with asihttprequest ?
I want to track the spe开发者_运维知识库ed 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
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];
}
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
精彩评论