Ios 非主线程不等待委托方法完成

Ios 非主线程不等待委托方法完成,ios,multithreading,Ios,Multithreading,我正在尝试使用应用程序中的线程调用web服务并从中获取数据 连接D完成加载 委托方法,但非主线程不会等待委托方法完成 以下是线程的代码: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Here your non-main thread. [self Login_WS]; //slee

我正在尝试使用应用程序中的线程调用web服务并从中获取数据

连接D完成加载

委托方法,但非主线程不会等待委托方法完成

以下是线程的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //Here your non-main thread.
                [self Login_WS];
                //sleep(10);

                dispatch_async(dispatch_get_main_queue(), ^{
                    //Here you returns to main thread.
                    //[MMProgressHUD updateProgress:1.f];
                    [MMProgressHUD dismissWithSuccess:@"done"];
                    DetailOfMenu *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
                    //[colorVC setColor:color];
                    UINavigationController *colorNavi1 = [[UINavigationController alloc] initWithRootViewController:detail];
                    [self.qp_splitViewController setRightController:colorNavi1];

                    MenuTableView *colorVC = [self.storyboard instantiateViewControllerWithIdentifier:@"menu"];
                    //[colorVC setColor:color];
                    UINavigationController *colorNavi = [[UINavigationController alloc] initWithRootViewController:colorVC];
                    [self.qp_splitViewController setLeftController:colorNavi];
                });
            });
下面是web服务的代码:

-(void)Login_WS{

    flag =1;

    NSString* ENC_UserName = [self ENC:Username.text];
    NSString* ENC_Password = [self ENC:Password.text];

    NSString *envelopeText = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                              "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
                              "<soap12:Body>\n"
                              "<GetReportsNames xmlns=\"http://tempuri.org/\">\n"
                              "</GetReportsNames>\n"
                              "</soap12:Body>\n"
                              "</soap12:Envelope>\n",];

    //envelopeText = [NSString stringWithFormat:envelopeText, txt1.text];
    NSData *envelope = [envelopeText dataUsingEncoding:NSUTF8StringEncoding];

    //NSLog(@"URL in Call_WS in SplashScreen %@",Var.url);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];


    [request addValue:@"http://tempuri.org/GetReportsNames" forHTTPHeaderField:@"SOAPAction"];


    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:envelope];
    [request setValue:@"application/soap+xml; charset=utf-8"
   forHTTPHeaderField:@"Content-Type"];

    [request setValue:[NSString stringWithFormat:@"%d", [envelope length]]forHTTPHeaderField:@"Content-Length"];

    // fire away
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection)
        responseData = [NSMutableData data];
    else
        NSLog(@"NSURLConnection initWithRequest: Failed to return a connection.");

}
-(无效)登录\u WS{
flag=1;
NSString*ENC_UserName=[self-ENC:UserName.text];
NSString*ENC_Password=[self-ENC:Password.text];
NSString*envelopeText=[NSString stringWithFormat:@“\n”
“\n”
“\n”
“\n”
“\n”
“\n”
“\n”,];
//envelopeText=[NSString stringWithFormat:envelopeText,txt1.text];
NSData*信封=[envelopeText dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@“SplashScreen中调用的URL%@”,Var.URL);
NSMutableURLRequest*request=[NSMutableUrlRequestWithURL:[NSURL URLWithString:url]缓存策略:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[请求添加值:@”http://tempuri.org/GetReportsNamesforHTTPHeaderField:@“SOAPAction”];
[请求设置HttpMethod:@“POST”];
[请求setHTTPBody:信封];
[请求setValue:@“应用程序/soap+xml;字符集=utf-8”
forHTTPHeaderField:@“内容类型”];
[request setValue:[NSString stringWithFormat:@“%d”,信封长度]]forHTTPHeaderField:@“内容长度”];
//开火
NSURLConnection*connection=[[NSURLConnection alloc]initWithRequest:request委托:self];
如果(连接)
responseData=[NSMutableData];
其他的
NSLog(@“NSURLConnection initWithRequest:未能返回连接。”);
}

注意:它在非线程状态下工作正常。

您应该在调用
[self-Login\WS]后执行当前的UI部分当web服务调用返回响应或失败时

由于webservice调用也是异步的,Login\u WS方法在您创建它之后立即返回,因此您的UI代码将在那里被调用。而是实施
nsurlconnectionelegate
协议,并在以下位置更新用户界面:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
或:


不确定要在代码中的哪个点等待,以及要阻止哪个部分执行。您的问题属于同步类别,其中一个线程必须等待其他部分完成工作。为此,您可以使用NSCondition。你会在谷歌上找到很好的例子:-)

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error