Reputation: 300
I am working in an application in which I Am getting data from web service in json format for each and every page and also sending data to remote data base.In this process my application is getting slow .Until the data is fetched from web service the next view is not loaded.It is consuming a lot of time, is there any way to fetch data and show view simultaneously.I heard about thread programing but I Am not familiar with it .Please suggest me which technique I should adopt to make my application faster.any help will be appreciated.
Upvotes: 1
Views: 719
Reputation: 80265
For your purposes, it would be easiest to just use the standard NSURLConnection
. It is asynchronous out of the box. In the completion handler you can e.g. update your UI, or notify it to update itself.
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request
queue:self.queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// do something with data or handle error
}];
Upvotes: 0
Reputation: 779
Fo this you can do like do your all downloading and uploading of data in background process so user does not need to wait until the data completed them task.
for background process you can easily use with
[self performSelectorInBackground:@selector(runProcessInBackground:) withObject:nil];
or you also can refer this https://stackoverflow.com/questions/9949248/iphone-sdk-running-a-repeating-process-in-a-background-thread.
and also u can use pagination in the appliaction for displaying data like in first call u will just receive first 100 records from the web service so that will take small time period and after 100 fetched then load another when page come in end so with this also u can increase your app. speed.
hope this will help you.
Upvotes: 0
Reputation: 10244
You can use Grand Central Dispatch or an NSInvocationOperation to do the loading in a background thread:
dispatch_queue_t q = dispatch_get_global_queue(0, 0);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(q, ^{
PagedSearchResult *result = // something which takes a while to complete
dispatch_sync(main, ^{
// update the UI
});
});
Take a look at Ray Wenderlich's Grand Central Dispatch tutorial.
Upvotes: 3