【发布时间】:2019-12-26 18:18:32
【问题描述】:
我正在从另一个方法调用一个方法。该方法在中间退出。完成一些任务后,它会运行剩下的方法。
-(void)stateMethod{
[self.pickerView selectRow:0 inComponent:0 animated:YES];
lblTitle.text=@"State";
self.stateTF.text=@"";
self.stateTF.inputView=_pickerView;
[self.stateTF setInputAccessoryView:toolBar];
NSString * method=@"***************************?countryID=";
NSString *urlString=[NSString stringWithFormat:@"%@%@%@",MAIN_URL,method,_countryId];
NSURL *url_ac=[[NSURL alloc]initWithString:urlString];
NSMutableURLRequest *request_ac=[[NSMutableURLRequest alloc]initWithURL:url_ac];
[request_ac setValue:loginUser.acessTokenStr forHTTPHeaderField:@"access_token"];
[NSURLConnection sendAsynchronousRequest:request_ac queue:[NSOperationQueue currentQueue] completionHandler:
^(NSURLResponse ac_response, NSData acData, NSError *connectionError) {
if (connectionError)
{
NSLog(@"ERROR CONNECTING DATA FROM SERVER: %@", connectionError.localizedDescription);
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *responseString = [[NSString alloc] initWithData:acData encoding:NSUTF8StringEncoding];
[self parseStateListResult:acData];
});
}
}];
}
我想要在调用状态方法时得到响应。基于状态方法响应,我在调用 [self statemethod] 后正在执行一项任务。该任务需要 'state methodresponce. That task is executing before getting the data from state method.
The method exits afterNSURLConnection` 行。我想异步运行该方法。请帮帮我。
【问题讨论】:
-
“编译器将方法留在中间”?什么意思?
-
我从
another method打电话给stateMethod。编译器将编译到NSURLConnection行,之后它将退出该方法并执行该行之后的一些任务,即[self stateMethod]; .再次编译器进入该方法并执行剩余的行(即NSURLConnection之后)。 -
当你说“编译”时,你确定你不是指“运行”。您似乎也对异步操作的概念感到困惑,因为您所描述的听起来是对的;即在后台建立连接,并在收到响应时调用完成处理程序。
-
你说得对。它只在应用程序运行时发生。我放调试器看应用的流程,那个时候方法会在中间退出。
-
但这是正确的。连接将在后台线程中发生,
stateMethod将退出。
标签: ios objective-c asynchronous semaphore synchronous