oridahan
oridahan

Reputation: 574

Delay when loading data form a website asynchronously

I wrote an app that parses data from a website into a tableview.

I want the app to block the UI until it is done downloading the data and display it to the table view, so I use MBProgressHUD to show feedback for the user when the parsing happens.

I wrote the following method to download the data asynchronously:

- (void)getDataFromUrlString:(NSString *)string
{
   NSDate *dateAtStartOfGetDataFromString = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
   NSLog(@"dateAtStartOfGetDataFromString = %@", dateAtStartOfGetDataFromString);

   NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:string];

   NSURL *url = [NSURL URLWithString:databaseURL];

   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] > 0 && error == nil){
        [self loadReports:data];
     }else if ([data length] == 0 && error == nil){

     }else if (error != nil && error.code == NSURLErrorTimedOut){ //used this   NSURLErrorTimedOut from foundation error responses

     }else if (error != nil){

     }
 }];
 NSDate *dateAtEndOfGetDataFromString = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
 NSLog(@"dateAtEndOfGetDataFromString = %@", dateAtEndOfGetDataFromString);

}

This is the loadReports: method:

- (void)loadReports:(NSData *)data {
    //...Get the data into the table view data array

    [HUD hide:YES];
    _reports = newReports;

    [self.tableView reloadData];
    NSDate *dateAfterLoadReports = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSLog(@"dateAfterLoadReports = %@", dateAfterLoadReports);
}

The app loads very fast and show the HUD until the loading of the data to the table view completes successfully.

The problem is that it takes a little bit too long (5 seconds) until the data shows.

I checked the time that it take the app to load and parse the data using NSDate:

2013-01-31 18:06:52.508 TrafficReport[4472:c07] dateAtStartViewDidLoad = 2013-01-31 16:06:52 +0000
2013-01-31 18:06:52.513 TrafficReport[4472:c07] dateAtStartOfGetDataFromString = 2013-01-31 16:06:52  +0000
2013-01-31 18:06:52.513 TrafficReport[4472:c07] dateAtEndOfGetDataFromString = 2013-01-31 16:06:52 +0000
2013-01-31 18:06:52.516 TrafficReport[4472:c07] dateAtEndViewDidLoad = 2013-01-31 16:06:52 +0000
2013-01-31 18:06:52.664 TrafficReport[4472:4a03] dateAfterLoadReports = 2013-01-31 **16:06:52** +0000
2013-01-31 18:06:57.569 TrafficReport[4472:4a03] dateAtStartCellForRow = 2013-01-31 **16:06:57** +0000

As you can see after loadReports there is a delay of 5 seconds until it executes the cellForRow method, and I don't know what it is and how to fix it.

Any ideas?

Upvotes: 0

Views: 139

Answers (1)

kovpas
kovpas

Reputation: 9593

You must perform all UI changes in main thread:

....
if ([data length] > 0 && error == nil){
    dispatch_async(dispatch_get_main_queue(), ^{
        [self loadReports:data];
    });
}
....

Upvotes: 2

Related Questions