我正在用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