我有一个需要等待完成处理的结果返回的该值将完成处理程序内设置一个布尔函数:等到完成处理程序结束以返回BOOL?
-(BOOL) fetchFeed
{
......
NSURLSessionDataTask *dataTask =
[sharedSessionMainQueue dataTaskWithRequest:req completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if(!jsonObject)
{
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){[self dismissViewControllerAnimated: YES completion: nil];}];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Connection problem." message:@"Please check your internet connection and trying logging in again." preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
_internetFailed = YES;
}
else
{
_internetFailed = NO;
}
});
}];
[dataTask resume];
return _internetFailed;
}
的问题是,现在_internetFailed之前返回完成处理程序的结束。我尝试将“return _internetFailed”移动到完成处理程序中,但这是不允许的。问题:我应该如何构造这个以便返回等待完成处理程序的结束?
我环顾四周,但在完成处理程序类型范例中找不到完成处理程序。还有什么其他选择?在此先感谢您的任何建议。
你可以使用信号量 – Andy 2014-12-04 20:29:24
@安迪谢谢,我甚至不知道那些存在。将研究它。 – 2014-12-04 20:48:24