title: Lag when parsing data from a website - Objective-C
body: <p>I wrote an app that parses data from a website into a tableview.</p>
  
  <p>I also useMBProgressHUD to show feedback for the user when the parsing happens.</p>
  
  <p>I call the loading method from the viewDidLoad:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  
      noConnectionAlert = [[UIAlertView alloc] initWithTitle:@"אין מידע להצגה"
                                                 message:@"אין אפשרות להתחבר לאינטרנט."
                                                delegate:self cancelButtonTitle:@"אישור"
                                       otherButtonTitles: nil];
  
      HUD = [[MBProgressHUD alloc] initWithView:self.view];
      HUD.labelText = @"טוען...";
  
      [self.view addSubview:HUD];
      [HUD showWhileExecuting:@selector(loadReports) 
                    onTarget:self withObject:nil animated:YES];
  
      BOOL hasConnection = [self hasInternetConnection];
  
      if (!hasConnection) {
         [noConnectionAlert show];
      } else {
         [self loadReports];
      }
  
  }
  </code></pre>
  
  <p>I have a TabBar, so this loading happens in the first and second views.
  The problem is that I notice a lag (when there is a slow internet connection) when the app is luanching and when I tap the second tab in the TabBar.</p>
  
  <p>Is there another way so that the app will launch (or move to the next tab when tapped)  as fast as it can (and show the HUD as long as it takes it to load the data)?</p>
  
  <p>edit:</p>
  
  <p>here is loadReports:</p>
  
  <pre><code>- (void)loadReports {
  NSURL *reportsUrl = [NSURL URLWithString:@"http://www.iba.org.il/moked/index.aspx?   classto=mokedTraffic"];
  NSData *reportsHtmlData = [NSData dataWithContentsOfURL:reportsUrl];
  
  
  NSString *encodedStringData = [[NSString alloc] initWithData:reportsHtmlData    encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew)];
  NSLog(@"%@", encodedStringData);
  NSData *reportsHtmlDataEncoded = [encodedStringData dataUsingEncoding:NSUTF16StringEncoding];
  
  TFHpple *reportsParser = [TFHpple hppleWithHTMLData:reportsHtmlDataEncoded];
  NSString *reportsXpathQueryString = @"//table//tr//td";
  NSArray *reportsNodes = [reportsParser searchWithXPathQuery:reportsXpathQueryString];
  
  for (int i = 0; i &lt; reportsNodes.count - 1; i++) {
      TFHppleElement *nodeFirstChild = [(TFHppleElement *)reportsNodes[i] firstChild];
      if ([[nodeFirstChild tagName] isEqualToString:@"text"]) {
          NSLog(@"%@", reportsNodes[i]);
          NSLog(@"%@", nodeFirstChild);
      }
  }
  NSMutableArray *newReports = [[NSMutableArray alloc] initWithCapacity:0];
  
  NSString *thisAttribute = [[NSString alloc] init];
  NSDictionary *thisNode = [[NSDictionary alloc] init];
  for (TFHppleElement *element in reportsNodes){
      NSString *str = [[element firstChild] content];
      if (str != NULL) {
          TFHppleElement *firstChild = [element firstChild];
          NSArray *childStrong = [element childrenWithTagName:@"strong"];
          NSLog(@"%@", childStrong);
          NSString *mokedContent;
          for (TFHppleElement *strongElement in childStrong){
              NSLog(@"%@", [[[strongElement firstChild] firstChild] content]);
              mokedContent = [[[strongElement firstChild] firstChild] content];
          }
  
          thisNode = [element attributes];
          thisAttribute = (NSString *)[thisNode objectForKey:@"width"];
          if (thisAttribute == NULL) {
              NSArray *spanChildren = [element childrenWithTagName:@"span"];
              if (spanChildren.count &gt; 0) {
                  TFHppleElement *spanChild = [spanChildren objectAtIndex:0];
                  NSString *spanContent = [[spanChild firstChild] content];
                  NSRange firstRange = [spanContent rangeOfString:@"\u00a0("];
                  BOOL isFound = NO;
                  if (firstRange.length &gt; 0){
                      isFound = YES;
                      NSLog(@"String contains");
                  }
                  else {
                      NSLog(@"No found in string");
                  }
                  NSString *result = [spanContent stringByReplacingCharactersInRange:firstRange    withString:@""];
                  NSRange secondRange = [result rangeOfString:@")\u00a0"];
                  NSString *time = [result stringByReplacingCharactersInRange:secondRange withString:@""];
                  NSLog(@"%@", time);
  
                  ORDKolIsrael *kolIsraelTraffic = [[ORDKolIsrael alloc] init];
                  [newReports addObject:kolIsraelTraffic];
  
                  NSString *reportFinal = [firstChild content];
                  kolIsraelTraffic.kolIsraelReport = reportFinal;
  
                  kolIsraelTraffic.kolIsraelTime = time;
                  NSLog(@"%@", kolIsraelTraffic.kolIsraelReport);
              }
  
          }
      }
  }
  _reports = newReports;
  [self.tableView reloadData];
  }
  </code></pre>
  
  <p>edit(2):</p>
  
  <p>I added a method to get the data asynchronously:</p>
  
  <pre><code>    - (void)getDataFromUrlString:(NSString *)string
      {
         //Set database address
      NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:string];
  
      //prepare NSURL with newly created string
      NSURL *url = [NSURL URLWithString:databaseURL];
  
      //AsynchronousRequest to grab the data
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  
      [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
       {
           if ([data length] &gt; 0 &amp;&amp; error == nil){
               [self loadReports:data];
           }else if ([data length] == 0 &amp;&amp; error == nil){
  
           }else if (error != nil &amp;&amp; error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses
  
           }else if (error != nil){
  
           }
       }];
     }
  </code></pre>
  
  <p>I call this method from the viewDidLoad but the tableview is not getting the data (empty table) despite the fact that when I debugged the code I could see the desired data in the CellForRowAtIndexPath.
  Any idea why?</p>