0

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.

Jean Paul
  • 2,389
  • 5
  • 27
  • 37

2 Answers2

2

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

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated]
    MyWebService *webservice = [MyWebService myWebService];
    webservice.delegate = self;
    [webservice getMyDataWithMyNumber:mySharedNumber myOldNumber:temp];
    [webservice getvDetailsWithmyData:myData SmartJoinderNumber:myNumber];
    //assume internally these web service calls aggregate into one response
}

- (void) myWebService:(MyWebService *)webService didRespondWith:(NSData *)data {
    MyViewController *myViewController = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    [self.navigationController pushViewController: myViewController animated:YES];
    [myViewController release];
}
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • I am not familiar with delegation patterns. Could you provode some examples or links. – Jean Paul Mar 21 '12 at 18:07
  • Read the section in the Cocoa Fundamentals Guide: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18 – bandejapaisa Mar 21 '12 at 18:53
  • I dont understand how using delegates will make sure that all other code will be executed only after the web service calls are completed. – Jean Paul Mar 21 '12 at 19:02
  • 1
    Because you don't put all of that code in one method. Webservices tend to be put in asynchronous code, as you don't know how long they will take to respond. So as soon as you execute the web service call, it will return immediately, and process in a background thread, and will callback at a later time. This callback is calling a method on the delegate, which signals that the web service is complete - and the calling code can continue what it needs to do. – bandejapaisa Mar 21 '12 at 19:47
1
In your case, Apple has recommended to use delegate pattern.

Here's an answer about delegate, that might help you to understand delegate concepts:
http://stackoverflow.com/questions/1089737/parsing-xml-in-cocoa/1090170#1090170
Invincible
  • 412
  • 3
  • 19
  • I am using gdatdaxml parser. Could you please provide a more helpful link . thanks – Jean Paul Mar 21 '12 at 18:39
  • 1
    I think, in your case use delegate pattern. If you want to know how delegate work then let me know. – Invincible Mar 22 '12 at 14:55
  • 2
    http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/81489-how-why-implement-delegation-pattern.html Have a look and if you like then accept my answer – Invincible Mar 22 '12 at 16:39