gpt4 book ai didi

objective-c - 等待代码执行完毕

转载 作者:太空狗 更新时间:2023-10-30 03:39:14 24 4
gpt4 key购买 nike

我想知道在 Objective 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];

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

images = [ws callWebService:api :data];

images = [ws returnArray];

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

最佳答案

你很容易做到,如下所示,

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

}

关于objective-c - 等待代码执行完毕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3615939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com