AmJa
AmJa

Reputation: 798

Calling web service from another class

I will directly start with example to explain my problem: I have a Webservice Class that has all delegate methods used to call web service and then parse the responseXML. We have written one method in this class that initiates the webservice call and parsing of XML. The other two classes suppose A & B that want to use this webservice by calling the above method.

//Class A
#import "Webservice.h"
- viewDidLoad
{
Webservice *ws = [[Webservice alloc] init];
[ws callWebservice];
statement 1..
Statement 2..
...   
}

Now what happens is [ws callWebservice]; this method gets called but returns without executing the following delegate methods written in Webservice class

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection

after executing statement 1, statement 2 in class A , then the control goes back to above these methods. Problem: How to call above three methods before the execution of statement 1, statement 2 as I want to use the parsed data in these two statements.
Please help. Thnx

Upvotes: 2

Views: 1273

Answers (2)

AmJa
AmJa

Reputation: 798

I have found the answer. What I have done is called the webservice on another thread, then added the calling in currentrunloop. Then the related connection methods were called step by step. If someone is looking for the same let me know I will write in details. Thnx for all your replies. they worked for me. rgds.. amit

Upvotes: 0

drvdijk
drvdijk

Reputation: 5554

Your webservice class probably hits the web asynchronously, allowing the calling thread to directly continue executing its statements. The direct (but not so nice) solution would be to not have that webservice class call net-functions asynchronously, but then your calling method would hang which is probably not what you want.

Your webservice class probably registers itself as the delegate of a connection class. I'd make a protocol WebserviceDelegate, in which you define some methods that get called on the caller of the webservice, something like this:

@protocol WebserviceDelegate
- (void)webserviceCallFinished;
@end

Your Webservice class would then have a delegate property:

@interface Webservice {
    id<WebserviceDelegate> delegate;
    // other ivars
}
@property (assign) id<WebserviceDelegate> delegate;
// other methods
@end

Then I'd register the calling class (Class A) as the Webservice instance's (ws) delegate (ws.delegate = self). After that call the webservice ([ws callWebservice]). After that, show a spinning animation or something, but right after that, the viewDidLoad method is done.

Then, I'd have those connection:didReceiveResponse:resonse, connection:didReceiveData:data, and connectionDidFinishLoading: delegate methods call methods on the webservice delegate.

So, your calling class would look like:

- (void)viewDidLoad
{
    Webservice *ws = [[Webservice alloc] init];
    ws.delegate = self;
    [ws callWebservice];
    [ws release];
}
- (void)webserviceCallFinished
{
    // Statement 1
    // Statement 2
}

While your Webservice class would call the delegate.webserviceCallFinished method when approprate

Upvotes: 2

Related Questions