当 [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:?答案 0 :(得分:0)
在第一个示例中,未调用委托方法,因为API不包含用于设置委托的参数。
在带有完成处理程序的API中,在执行完成处理程序时完成连接。