from one screen on click of a button i am navigating on other screen, i am navigating properly but screen is getting hang till the screen to which i am navigating gets web service response (in viewDidLoad i am calling a web service) how to fix this Thanks
2 Answers
Move web service call to viewWillAppear
or viewDidAppear
, so call will initiate after controller view appears on screen.
Ideally you should perform the web service call in the background i.e. not on the main thread. Use NSOperation
and NSOperationQueue
or AFNetworking
or Grand Central Dispatch. You can then initiate the call in viewDidLoad
itself.
Here are a few links that can get you started.
- How To Use NSOperations and NSOperationQueues
- Networking Made Easy With AFNetworking
- iOS Quick Tip: Interacting with Web Services - for using GCD
Hope that helps!

- 13,202
- 7
- 53
- 71
-
no can't move webservice in viewWillAppear beacuse view will appear call every times so it;s not better solution . – Jitendra Sep 12 '13 at 07:07
-
@JitendraDeore A simple flag can ensure that it does not get called every time. I have also suggested with better approach. – Amar Sep 12 '13 at 07:08
-
k Amar so webservice get called only once. thats correct now and check out my solution i am also providing better approch for him. – Jitendra Sep 12 '13 at 07:10
This is happen because of webservice contains heavy data that's whay you need to try this.
Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image again and again.
As suggested by many developers go for SDWebimage and it does include the above strategy to download the images files .You can load as many images you want and the same URL won't be downloaded several times as per the author of the code
Example on
[NSURLConnection sendAsynchronousRequest:queue:completionHandler:
NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
//doSomething With The data
else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
//time out error
else if (error != nil)
//download error
}];
Then use NSCache...

- 5,055
- 2
- 22
- 42
-
@Jitendra You seem to be repeating [this answer](http://stackoverflow.com/a/18507182/1407017). You could have just added a comment with the link. – Amar Sep 12 '13 at 07:18
-
ya i know taht is repaeat but this is write way to do this that's why i post this answer again.. – Jitendra Sep 12 '13 at 07:19
-
When i am using detachNewThreadSelector to call web service in viewDidLoad NSURLConnection delegates is not getting called. How can i correct it. – Aanchal Chaurasia Sep 12 '13 at 07:23