-1

In my ios app i am using GCD calls to download some data in background threads , but its not updating UI instantly, I should touch the screen to update UI , following is my code, shall I know why table view is not reloaded though I am trying to Update in Main thread

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //background processing goes here
         // Downloading JSON data from Web APIs
        dispatch_async(dispatch_get_main_queue(), ^{
                //update UI here

                //Reloading my table view
                 [objTableView reloadData];
        });
});

I have also tried runOnMainQueueWithoutDeadlocking instead of dispatch_async(dispatch_get_main_queue() but its of no use

void runOnMainQueueWithoutDeadlocking(void (^block)(void))
{
    if ([NSThread isMainThread])
    {
        block();
    }
    else
    {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
}
Ravi Kiran
  • 691
  • 3
  • 9
  • 22
  • 1
    What's your actual code for `reloading my table view` and `//update UI Here` please share all relevant code – Popeye Mar 10 '15 at 13:14
  • Is method `reload` is your own method? `UITableView` contains only `[self.tableView reloadData]` – Szu Mar 10 '15 at 13:30
  • @Szu ,,its reloadData only – Ravi Kiran Mar 10 '15 at 13:31
  • Have a look at http://stackoverflow.com/questions/661031/how-to-reload-a-uitableview-while-i-am-looking-at-it and http://stackoverflow.com/questions/17348132/reload-data-of-uitableview-in-background All these answers are basically saying you should be doing just `dispatch_async(dispatch_get_main_queue(), ^{ [self.objTableView reloadData]; });` Have you set your tableView up correctly with the dataSource and delegate? – Popeye Mar 10 '15 at 13:32
  • does `objTableView` is not nil? – Szu Mar 10 '15 at 13:32
  • can you please try "__block UITableView *tableView = objTableView;" before going into blocks? – Mert Buran Mar 10 '15 at 13:33
  • So basically change `[objTableView reload];` to `[objTableView reloadData]` because according the Apple documentation there is no `reload` it's `reloadData` https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/doc/uid/TP40006943-CH3-SW61 – Popeye Mar 10 '15 at 13:34
  • please show, how do you update UI – f3n1kc Mar 10 '15 at 13:53

2 Answers2

1

Answer given before user decided to change the questions code

According to the Apple Documentation for UITableView there is no method called reload it should be reloadData. So you need to change the line [objTableView reload]; to [objTableView reloadData]; as per the Apple Documentation around UITableView reloadData:. Also if you checkout How to Reload a UITableView While I am Looking at It and Reload data of UITableView in background these also say it's [objTableView reloadData];.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
0

Try to put

yourtableviewdelegate.layoutIfNeeded() 

after the reload of the tableview.

mastro35
  • 133
  • 9