0

我有一个getnamefornumbers调用基于soap的Web服务(生成sudzc)的方法,它返回一些我存储在数组中以供使用的数据。但问题是,当我调用该方法时,它需要时间来执行,并且在该方法也执行之后编码,这会导致空数组。

当此方法完成其工作然后执行其余代码时,我该怎么做。

4

2 回答 2

0

您应该使用NSURLConnection委托方法。在异步环境中,这是正常行为:

  1. 您拨打电话(以异步方式)
  2. 应用程序继续运行(在您进行 1. 调用后,程序继续执行其余说明)

所以你必须有两个解决方案,让它同步,所以你只会在答案到来后继续(在你的情况下,数组被填满),我可能会不鼓励。或者,您将其设为异步,并在实际拥有该数组时使用该数组。

至于如何实施的具体细节,必须提供更多细节,以便我为您提供建议。


更新 1.0

-(void)requestConnectionToServer{

   NSURL *url= [NSURL URLWithString:@"myWebServiceURL"];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];

    self.reference=aReference;

    [theRequest setHTTPMethod:@"GET"];

    [theRequest setTimeoutInterval:20.0];
    [NSURLConnection connectionWithRequest:theRequest delegate:self];
}

#pragma mark NSURLConnectionDelegate Implementation

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"Response:%@",[[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding]);

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"ERROR with theConenction %@",error);
}

更新 2.0

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^{

        myArray = [MyWebServiceAccess getnamefornumbers];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [myArray makeSomething];
        });
        });
于 2012-07-02T08:35:52.307 回答
0

您必须使用自定义委托。您应该定义协议并将当前类委托给负责执行 getnamefornumbers 的类。操作完成后,您应该返回调用者类。

这是协议的示例http://mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

于 2012-07-02T08:36:29.263 回答