On a button click, I am calling a web service and after that I am pushing a UIViewController. My UIViewController should load with the data obtained from the web service. But currently, before my web service is called, the UIViewController is being pushed. What can I do to make sure that my UIViewController is not loaded before all the web service calls are made and data retrieved.
Here is the code I am using.
MyWebService *webservice = [MyWebService myWebService];
webservice.delegate = self;
[webservice getMyDataWithMyNumber:mySharedNumber myOldNumber:temp];
[webservice getvDetailsWithmyData:myData myNumber:myNumber];
MyViewController *myViewController = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
[self.navigationController pushViewController: myViewController animated:YES];
[myViewController release];
Edit: The UIViewController should be pushed only after both web services are called.
I am passing an array to the new UIViewController. The array objects are added during the web service call. I cant figure out a way to do this. Need help. Thanks.
Your web service needs to call back to the view controller when it has completed.
This is usually done using a Delegate pattern, but there are other techniques you could use.
Your first view controller would pass itself as a delegate to MyWebService. MyWebService does what it needs to do, and when it is done it calls a method on its delegate, the view controller.
In this callback method, you could then push the next view controller.
You should also consider the user experience with this. A user want’s a responsive device, or at least some indication something is happening. So when calling the web service, show a loading indicator. Alternatively, push the next view controller immediately, and then call the web service from the next view controllers viewWillAppear method (again show some sort of loading feedback).
.. I just re-read and noticed there is more to it. You have multiple separate web service calls. Are those 2 always called together? You could use a bool flag on return of each one, and only push if both have returned. I’d rather push the new view controller straight away, load them both and let them return independently to the new view controller.
UPDATED WITH EXAMPLE