Reputation: 2285
In my recent application for Web service calling I am using AFNetworking -> AFHTTPClient.
I need to show loader if web service response time is larger then 1 second, Else I just need to call web service.
How can I calculate this time while using AFNetworking. With NSURLConnection I can do something like [response expectedContentLength]
and caclulate some how the time require to download the file. but what about AFNetworking.
Upvotes: 0
Views: 493
Reputation: 2987
You can set progress block for AFNetworking Operation
like for AFDownloadRequestOperation operation
From this block you can get expected content length and the size of packets you are receiving from that you can calculate time required for complete operation (considered network speed will remain same)
But this cant be done before the receiving of data from web service (Like you get 200 bytes in 1 or 0.1 second) then divide packet received by time and get speed of network and then Calculate time for whole data download (web service operation)
[operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
NSLog(@"Operation%i: bytesRead: %ld", 1, (long)bytesRead);
NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
}
}];
Upvotes: 4