繁体   English   中英

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

[英]non-main thread does not wait for delegate methods to finish

我正在尝试使用应用程序中的线程来调用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.");

}

注意:在无线程的情况下,它工作正常。

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

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

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

要么:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM