当[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的调用一起调用?