我正在用AFNetworking发出JSON请求,然后调用[operation waitUntilFinished]以等待操作和成功或失败块。但是,它似乎,虽然右下降 - 在日志消息方面,我得到 “0”, “3”, “1”,而不是 “0”, “1”, “3”等待完成块在AFNetworking请求中完成
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://google.com"]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFFormURLParameterEncoding;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:@"query", @"q", nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:[url path] parameters:params];
NSLog(@"0");
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *innerRequest, NSHTTPURLResponse *response, id JSON) {
NSLog(@"1");
gotResponse = YES;
} failure:^(NSURLRequest *innerRequest, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"2");
gotResponse = YES;
}];
NSLog(@"Starting request");
[operation start];
[operation waitUntilFinished];
NSLog(@"3");
似乎对'[operation waitUntilFinished]'的调用不会等待完成块。 AFJSONRequestOperation.m使用'dispatch_async'执行它们,我认为它成为单独操作的一部分。这是否正确,是否有解决方法? – Kamran