0

I have this code block:

    +(void)requestPath:(NSString *)path onCompletion:(RequestCompletionHandler)complete
{
    // Background Queue
    NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];

    // URL Request
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]
                                                  cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                              timeoutInterval:10];

    // Send Request
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:backgroundQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               if (complete) complete(result,error);
                           }];


}

This code works to pull data for my app with no problems 99% of the times I use it. However, sometimes (but not always) on UIRefreshControl refresh requests the completion handler in NSURLConnection sendAsynchronousRequest is not hit and this causes my app to crash.

I could be requesting the same data, and at times the completion handler is hit but other times on refresh it will not work.

I'm not sure why this is happening.

Any help would be much appreciated.

Thanks!

user2312407
  • 67
  • 1
  • 11

1 Answers1

0

Handle the error case properly as

 [NSURLConnection sendAsynchronousRequest:request
                                       queue:backgroundQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error)
                               {
                                   NSLog(@"%@",[error localizedDescription]);

                               }
                               else
                               {
                                   NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   //if (complete) complete(result,error);
                               }

                           }];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101