1

I am using this block method to load my data from the server...

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[myString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]
                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                        timeoutInterval:60.0];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
 sendAsynchronousRequest:urlRequest
 queue:queue
 completionHandler: ^( NSURLResponse *response,
                      NSData *data,
                      NSError *error)
 {
     [[mySingleton dictionaryUserDetail] removeAllObjects];
     [[mySingleton arrayUserDetail] removeAllObjects];

     if ([data length] > 0 && error == nil) // no error and received data back
     {
         NSError* error;
         NSDictionary *myDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
         [mySingleton setDictionaryUserDetail:[myDic mutableCopy]];

         NSArray *myArray = [myDic objectForKey:@"searchResults"];
         [mySingleton setArrayUserDetail:[myArray mutableCopy]];

         dispatch_async(dispatch_get_main_queue(), ^{
             [self userDetailComplete];
         });
     } else if
         ([data length] == 0 && error == nil) // no error and did not receive data back
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             [self serverError];
         });
     } else if
         (error != nil) // error
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             [self serverError];
         });
     }
 }];

I would like to be able to show the user a progress bar instead of just a spinner. If I were using delegates I assume I could have used the NSURLConnectionDelegate

connection:didReceiveData:

but I do not know how to do this with a block. Anyone have some genius insight?

sangony
  • 11,636
  • 4
  • 39
  • 55

1 Answers1

3

If you want to display a progress bar that shows the actual download progress then you have to use the delegate-based methods of NSURLConnection. sendAsynchronousRequest and sendSynchronousRequest do not provide methods for that.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • There is no way around that? – sangony Apr 06 '13 at 18:11
  • @sangony: I am quite sure. The completion block is only called when the download is finished (or some error occurred). There are no delegate methods called. - The delegate-based methods have more advantages, for example you can cancel a request. – Martin R Apr 06 '13 at 18:13