tags:

views:

45

answers:

2

I would like to know the easiest way to wait for code to finish execution within an objective c project because I am calling a webservice and retrieving the results and instead it is retrieving the results before the webservice has finished being called and filled.

Any suggestions please?

Btw this is my webservice code:

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];

[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[theRequest addValue:@"http://tempuri.org/GetCategory" forHTTPHeaderField:@"SOAPAction"];

NSString *msgLength=[NSString stringWithFormat:@"%i",[soapMessage length]];

[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPMethod:@"POST"];

[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

and the code I am using to call this method from the other class:

images = [ws callWebService:api :data];

        images = [ws returnArray];

now the problem is, that the second line is being executed before the first has finished

+1  A: 

You could use one of many of the Cocoa design patterns (Delegate, Notification, etc).

For instance you would trigger the method and wait until you receive a response back.

It looks like you are using an asynchronous request and for this case, you would need to wait until one of the delegate methods get notified that the request has finished (with error or success).

BTW, what does your request look like? Could you share some code to explain how you do the request and when and what you want to do?

Edited after the code was inserted:

You set self as the delegate of the request, and so you should be able to handle the responses.

Have a look at the NSURLConnection Class Reference. You will need to trigger your parser when the request finishes on these methos, for example:

– connection:didReceiveResponse:
– connection:didReceiveData:
– connection:didFailWithError:

Cheers,

vfn

vfn
that won't work for me since these methods are also part of that class and they are being called from the main class
Lily
Why does it work for everybody but not for you? These methods are called when the request finishes. Based on your code or you should set as delegate the class where you call your method. Do something like: [ws callWebService:api data:data delegate:self]; and implement the delegate methods on this class.
vfn
My problem is that I have 2 classes: lets say A and B. Now from A I need to call images = [b callWebService:api :data];images = [b returnArray];where b is an instance of B.Now I have the connection:didReceiveResponse in B but before it gets there through the first line of code, it is going to the second line of code
Lily
This is the problem of your code. a should be the delegate of the request, not b. You shouldn't do images = [b returnArray]; because it makes no sense since you relies on the request be completed to get the data.
vfn
+1  A: 

You do it easily as below,

-(void)aFunc {

Do Asynchronous A job...

while (A is not finished) {
// If A job is finished, a flag should be set. and the flag can be a exit condition of this while loop

// This executes another run loop.
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}

Do things using the A's result.

}
alones
I don't understand how I may use this.
Lily
Does this aFunc run under a child thread ?
Toro
No the above code is in the main thread.You can think A jos is the http requester (asynchronous..)The above code is very useful when you wait for async job.For more detail, please see http://blog.sallarp.com/iphone-ipad-wait-for-asynchronous-nsrunloop-task-to-complete/
alones
You mean that you would stop the flow, lock UI, etc. Until you get a result back? Are you serious? Do you know way are delegates, notifications, kvo, ...????
vfn
Another though... What do you mean by wait the asynchronous request? If you will keep waiting to proceed, why it's asynchronous? Asyc, AFAIK is to be used when you can say "you, do this and let me know when you finish to do it, because I can't afford to wait until you finish, here stoped, doing nothing".
vfn