1

I have video on my server and i want to know in how much time my video requires to download from server in the device. So that i can calculate the download time and check if server is slow or not. Any help would be appreciated.

Here is my code:

  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:[[Configuration sharedInstance] fatchTimeOut]];

  [GMURLConnection sendAsynchronousRequest:request queue:self.operationQueue type:type withSubType:subType completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

 //**********************Success Handling ********************
    if (type == OperationTypeMeta) {
        failCount = 0;
        NSError *error = nil;

        NSArray *serverResponse=[NSArray array];
        if(data)
            serverResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
       }];
Developer007
  • 437
  • 3
  • 12

3 Answers3

0

I suggest you to use AFHTTPRequestOperation. Like this:

AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead){

        }];
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
  • I cant use external sdk. i already have a block named sendasynchronousblock for getting data from server. [GMURLConnection sendAsynchronousRequest:request queue:self.operationQueue type:type withSubType:subType completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {}]; – Developer007 Sep 22 '14 at 06:34
0

You may use NSURLConnection with its delegate.

When download file first you get response from server which has total file size

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
     _expectedContentLength = [response expectedContentLength];
        if (_expectedContentLength==NSURLResponseUnknownLength)
            _expectedContentLength = 0;
}

After in - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data you may calculate downloading time from time spent to download portion of file and whole file size

bartl
  • 392
  • 2
  • 11
  • Actually i am using sendAsynchronousRequest block here. According to you i will hav to create a http connection. Can we get the time within the block? – Developer007 Sep 22 '14 at 06:53
0

You cannot use sendAsynchronousRequest and monitor connection status. As per apple docs:

To retrieve the contents of a URL using a completion handler block: If you do not need to monitor the status of a request, but merely need to perform some operation when the data has been fully received, you can call sendAsynchronousRequest:queue:completionHandler:, passing a block to handle the results.

So you have to implement NSURLConnection delegate. If you like block style(as I really really do), just create new class which implement delegate methods and takes NSURLRequest, status block and completion block as params and call them in appropriate delegate methods. This way you will have block-styled asynchronous requests with status updates.

If you do not know how to measure remaining time - each time didReceiveData: method is called append data length to class variable, and calculate how much time passed since last method receive. This way you have velocity(bytes per second). Knowing how big is your downloaded file just divide remaining size to your velocity and you'll get remaining time. There are some neat algoritms out there to get neat/steady remaining time instead of just jumping from 30secs to 2 mins(if we assume our internet connection is tricky). I've personally used @Ben Dolman's solution posted over here:

averageSpeed = SMOOTHING_FACTOR * lastSpeed + (1-SMOOTHING_FACTOR) * averageSpeed;

With some tweaking and playing with SMOOTHING_FACTOR I've accomplished great results.

Community
  • 1
  • 1
hris.to
  • 6,235
  • 3
  • 46
  • 55