Chris Suen
Chris Suen

Reputation: 312

Load UITableViewController before webservices

I am trying to load the data from web-services and insert into the UITableViewController.

I can do this successfully but the problem is, it will 'hang' for a short period at the first UIView before going to the UITableViewController, when it is loading the web-services from the internet. It will hang longer if the internet speed is slow.

Any chance I can showing up the empty UITableViewController first with a 'Loading' sign and then only start retrieving the data from web-services and reload the table?

Currently, I put the function used to call web-services in

- (void)viewDidLoad
{
    [super viewDidLoad];
    self._completeList = [[NSMutableArray alloc]init];
    self._completeList = [self getListFromWebServices];
}

Upvotes: 2

Views: 113

Answers (4)

Manfred Scheiner
Manfred Scheiner

Reputation: 399

The easiest solution would be to move these two lines of code into -(void)viewDidAppear:

- (void)viewDidLoad {
  [super viewDidLoad];
  self._completeList = [[NSMutableArray alloc]init];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated]; 
  self._completeList = [self getListFromWebServices];
  // Assuming that [self getListFromWebServices] is a blocking call.
  [self.tableView reloadData];
}

But I would recommend to load your data async using GCD:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) { 
      _completeList = [[NSMutableArray alloc] init];
      dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self._completeList = [self getListFromWebServices];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
      });
    }
    return self;
}

Upvotes: 0

Horst
Horst

Reputation: 1739

Addition to @Durgaprasasad Ans:

dispatch_async(backgroundQueue, ^{
        aResult = [self getListFromWebServices];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self updateMyUIWithResult: aResult];
        });    
    });

Upvotes: 0

Nitin Gohel
Nitin Gohel

Reputation: 49720

Best approach as my suggestion you have to use Grand Central Dispatch (GCD) like bellow as example

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

       //code for webservices calling

        dispatch_async(dispatch_get_main_queue(), ^{

           //reload you tableview here
           [self.tableview reloadData];
        });
    });

Upvotes: 5

Durgaprasad
Durgaprasad

Reputation: 1951

You would be storing data in array and in numberOfRows method using array.count.

You should alloc init array in viewDidLoad and start secondary thread to download data from net.

 [self performSelectorInBackground:@selector(getListFromWebServices) withObject:nil];

When download is complete call table reloadData on main thread.

[self.table performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

So before downloading dataArray count would be zero and you will get blank table

Upvotes: 0

Related Questions