connectionDidFinishLoading:未使用[NSURLConnection sendAsynchronousRequest:queue:completionHandler:

时间:2017-02-27 15:52:14

标签: ios objective-c xcode nsurlconnection sendasynchronousrequest

[NSURLConnection sendAsynchronousRequest:]的答案得到一个好的状态代码时,我想用 NSMutableURLRequest 启动一个请求。当我单独启动包含 NSMutableURLRequest executeForStatusOkMetod 时,它可以正常运行。

但是如果我使用 [NSURLConnection sendAsynchronousRequest:启动 executeForStatusOkMetod ,则永远不会调用 connectionDidFinishLoading:函数。

这是主要代码:

 NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    request.timeoutInterval = 10;
    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
        NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

        if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
            {
               // do stuff
                [[DataManagement sharedManager] executeForStatusOkMetod];
            }
    }];

这是一个名为:

的函数
(void) executeForStatusOkMetod
{
 NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
        NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [request setURL:[NSURL URLWithString:[set_send_order stringByAppendingString: url_string]]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setTimeoutInterval:timeOut];
        [NSURLConnection connectionWithRequest:request delegate:self]; 
}

为什么在 [NSURLConnection sendAsynchronousRequest:方法?

中调用 NSMutableURLRequest 时,不调用 connectionDidFinishLoading:

1 个答案:

答案 0 :(得分:0)

在第一个示例中,未调用委托方法,因为API不包含用于设置委托的参数。

在带有完成处理程序的API中,在执行完成处理程序时完成连接。