1

I have an NSURLSession that I use to load some data from the cloud. I use this call

  [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
 if (!error) {
     NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
     if (httpResp.statusCode == 200) {
        NSError *jsonError;
        myArray = [[NSArray alloc] init];
        myArray = [NSJSONSerialization JSONObjectWithData:data
                                                         options:NSJSONReadingAllowFragments
                                                           error:&jsonError];
     }
  }
}] resume];

Now some times I go through this and myArray is empty, so I want to know whether I can put a check inside that if myArray is empty retry to load the data!!

Eddie
  • 949
  • 1
  • 8
  • 15

1 Answers1

1

Just add an if statement to check if the array count is equal to zero and if so then retry the connection.

[[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
 if (!error) {
     NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
     if (httpResp.statusCode == 200) {
        NSError *jsonError;
        myArray = [[NSArray alloc] init];
        myArray = [NSJSONSerialization JSONObjectWithData:data
                                                     options:NSJSONReadingAllowFragments
                                                       error:&jsonError];
       //Check for an empty array
       if ([myArray count] == 0) {
           //retry
       }
     }
  }
}] resume];
werm098
  • 159
  • 1
  • 11