0

当[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]; 
}

为什么connectionDidFinishLoading:不与位于[NSURLConnection sendAsynchronousRequest:方法中的NSMutableURLRequest的调用一起调用?

4

1 回答 1

0

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

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

于 2017-02-27T16:07:42.017 回答