等待代码完成执行

Lil*_*ilz 6 objective-c

我想知道等待代码在目标c项目中完成执行的最简单方法,因为我正在调用Web服务并检索结果,而是在web服务完成调用和填充之前检索结果.

有什么建议吗?

顺便说一下这是我的网络服务代码:

    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];
Run Code Online (Sandbox Code Playgroud)

以及我用来从其他类调用此方法的代码:

images = [ws callWebService:api :data];

        images = [ws returnArray];
Run Code Online (Sandbox Code Playgroud)

现在的问题是,第二行是在第一行完成之前执行的

alo*_*nes 20

你可以轻松地完成如下操作,

-(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.

}
Run Code Online (Sandbox Code Playgroud)