Reputation: 909
I'm writing an iOS app that is getting data from a server. I have several ViewControllers
. I used to load data for that viewcontroller under the viewDidLoad
method
-(void)ViewDidload
{
[self loadData];
}
-(void)loadData
{
//calling to webservice caller class
}
But this reduces the app's performance. What is the best method to load data within a viewcontroller? For webservice callings, I have a seperate class. Within my loadData
method I call to that particular method inside the webservice calling class.
This is going to block my UI.
Upvotes: 0
Views: 295
Reputation: 263
I guess your interface is lagging. Try this:
-(void)ViewDidload
{
[NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
}
Upvotes: 0
Reputation: 8225
What do you mean with "this reduces the app performance". Your app is lagging when you are calling your webservice? This is not because you are calling that in viewDidLoad
this is because you are doing that in the main thread.
To call your webservice you can use:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Call here your web service
dispatch_sync(dispatch_get_main_queue(), ^{
// push here the results to your ViewController
});
});
With this simple solution you are downloading data from your webservice in a separate thread. And pushing the data to your ViewController
with the mainThread. This code is not freezing your app. However you will have a moment that nothing happens. This is a good moment to use a UIActivityIndicatorVew
.
Upvotes: 2