Daniel van der Merwe
Daniel van der Merwe

Reputation: 1940

Adding loading function to objective c tableview

Currently I have a table that is updated by a server query, so it is not instant. Currently I populate the array in the viewDidLoad delegate method like so:

- (void)viewDidLoad
{
    [super viewDidLoad];    
    followers = [[User sharedManager] loadFollowers];    
}

However, what happens is that I press the button to modal transition into the tableview and it then waits for the query to succeed and only then pulls up the view. Obviously this is really annoying since it feels like it's crashing/lagging. I'd rather it pulls up the page instantly and has a "loading..." or an activity view while the query executes. How can I do this?

Upvotes: 0

Views: 73

Answers (2)

Paul.s
Paul.s

Reputation: 38728

If you do a lot of networking stuff you should look at classes like NSURLConnection or libraries like AFNetworking that make networking trivial.

You need to ensure that the networking stuff is not run on the main (UI) thread. You can do this with gcd like this

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  self.followers = [[User sharedManager] loadFollowers];
  dispatch_async(dispatch_get_main_queue(), ^{
    // update the UI now new data has been stored
    // e.g. [self reloadData];
  });
});

Upvotes: 0

hasan
hasan

Reputation: 24205

Start with the following(it will not look like lagging or crashing):

[self performSelectorInBackground:@selector(load_followers)];

-(void)load_followers
{
    followers = [[User sharedManager] loadFollowers]; 
}

You always have to do such queries in a background thread. you don't want the interface to freeze and preventing the user to interact with the application.

Upvotes: 1

Related Questions