1

So in my application I use a NSURLConnection to connect to a server and retrieve text. This is how I initiate my NSURLConnection:

NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
//Some other code
[serviceRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:postData];

//Force to return size
[serviceRequest setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
theConnection = [[NSURLConnection alloc] initWithRequest:serviceRequest delegate:self];

Now it works fine but I am trying to get a progress bar in my application to represent the progress of this request.

Now here are my didReceiveResponse and didReceiveData methods:

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    dlSize = [response expectedContentLength];
    NSLog(@"didReceiveResponse: %f", dlSize);
    [self.receivedData setLength:0]; 
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    dlProgress = ((float) [data length] / (float) dlSize);
    NSLog(@"dlProgress: %f", dlProgress);
}

So this is the issue. As you can see I have two NSLogs in there, but they are not called until the request is done. To be specific I am contacting a server to OCR something and once it is done OCRing and I get the text, that is when these two methods are called. Then the logs return something like this:

2013-07-30 22:50:09.201 app[39381:907] didReceiveResponse: 514.000000
2013-07-30 22:50:09.202 app[39381:907] dlProgress: 1.000000
2013-07-30 22:54:39.651 app[39381:907] didReceiveResponse: 305.000000
2013-07-30 22:54:39.651 app[39381:907] dlProgress: 1.000000

I am not sure why those methods aren't called during the request but maybe somebody on this site knows why? Anyway any tips/comments/suggestions are appreciated so I can get a UIProgressView hooked up with this.

Thanks!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • 2
    Progress is only useful with larger downloads. Content less than 1kB is a tiny spec of data that easily comes down in one chunk. Progress isn't useful until you are downloading at least 100's of kB. – rmaddy Jul 31 '13 at 04:16
  • That is true for the most part, but this request can take (on average) 10 seconds or more on the fastest speeds (LTE). If somewhere were to be doing this on a 3GS, it could take 15-20 seconds and personally I think it would be nice to have a progress bar, if possible. Is there any way to make it so that it is split into multiple chunks and update the progress that way? – SimplyKiwi Jul 31 '13 at 04:20
  • But progress is based on data length, not time. It could take a week or a second. It doesn't matter when you configure your progress view to be based on data length. There is no way to know how much time it will take so there is nothing you can do. Perhaps you could show an activity indicator until the initial response then show the progress view as data is actually retrieved. But again, progress is pointless with such small content. – rmaddy Jul 31 '13 at 04:24
  • Yes, I kind of do that now. I just have a spinning activity indicator. But as you kind of said, the didReceiveResponse/didReceiveData is called once the connection is finished so what I am guessing is that the chunk of data just came in all at once and that is why the delegate methods are being called when they are being called, right? – SimplyKiwi Jul 31 '13 at 04:28
  • Right. As I stated, your content is way too small to justify a progress indicator. 514 bytes (or 305 bytes) easily comes down in one chunk. I show progress views for data downloads and they only become useful when downloading much larger files. – rmaddy Jul 31 '13 at 04:31
  • see this [answer](http://stackoverflow.com/a/16454923/1704346) – Lithu T.V Jul 31 '13 at 05:56

1 Answers1

1

declare in yourViewController.h NSURLResponse *response; in this method write only:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse        *)aResponse
  {

    response = aResponse;   

   }

after (downData is a NSMUtableData)(progresso is UIProgressView):

     -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

       [downData appendData:data];

       float expectedLength = [response expectedContentLength];
       float currentLength = downData.length;

       progresso.progress = currentLength / expectedLength;

      if (currentLength/expectedLength == 1) {

          //do anything
      }

      }
Ilario
  • 5,979
  • 2
  • 32
  • 46