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

时间:2014-07-03 10:52:27

标签: ios multithreading

我正在尝试在我的应用中使用线程来调用Web服务并从

获取数据
  

connectionDidFinishLoading

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

这是线程的代码:

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.");

}

注意:它在非线程情绪下工作正常。

2 个答案:

答案 0 :(得分:0)

当Web服务调用返回响应或失败时,您应该在调用[self Login_WS];之后执行当前的UI部分。

由于webservice调用也是异步的,因此Login_WS方法在创建后立即返回,因此您的UI代码将在那里被调用。而是实现NSURLConnectionDelegate协议并在以下位置更新您的UI:

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

或:

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

答案 1 :(得分:0)

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

相关问题