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

我想用NSMutableURLRequest启动一个请求,当[NSURLConnection sendAsynchronousRequest的答案获得一个好的状态码。 当我单独启动包含NSMutableURLRequestexecuteForStatusOkMetod时,它完美地工作。

但是,如果我用[NSURLConnection sendAsynchronousRequest:]启动executeForStatusOkMetodconnectionDidFinishLoading:函数永远不会被调用。

这里是主要的代码:

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

这是所谓的function:

 (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调用?

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

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