NSURLSessionDataTask dataTaskWithURL完成处理程序未被调用 [英] NSURLSessionDataTask dataTaskWithURL completion handler not getting called
问题描述
我最近一直在学习Objective C,我决定尝试连接。
I have been learning Objective C lately, and I decided to try connections.
NSURLConnection的一切都很棒,直到我发现它已经过时,并试图工作与NSURLSession。
Everything was great with NSURLConnection, until I discovered it was outdated, and tried to work with NSURLSession.
我正在尝试一个非常简单的例子,但似乎无法让我的应用程序在完成块内运行代码。
I am trying a very simple example, but can't seem to get my app to run the code inside the completion block.
以下是使用的代码:
NSURL * url = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk"];
NSLog(@"2");
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSLog(@"3");
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
delegate: nil
delegateQueue: [NSOperationQueue mainQueue]];
NSLog(@"4");
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"11");
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
NSLog(@"22");
});
}];
NSLog(@"5");
[dataTask resume];
NSLog(@"6");
我得到主执行中打印的所有数字,但是从未执行completionHandler。我也尝试使用代表,但没有成功。
I get all the numbers printed from the main execution, but the completionHandler is never executed. I also tried this using a delegate, with no success.
提前致谢。
编辑
根据建议,我已将我的功能更改为以下内容:
EDIT As suggested, I have changed my function to the following:
-(void) doGET{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:[self url]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"11");
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
NSLog(@"22");
}];
[dataTask resume];
}
我的完成经理仍未运行。我也尝试使用sharedSession而不是传递配置但也没有运气。
My completion manager is still not getting run. I also tried with the sharedSession instead of passing a configuration but no luck either.
感谢您的帮助
推荐答案
如果您打算使用数据任务的完成块再现,而不是指定的
,如下所示:委托
> nil
If you're going to use the completion block rendition of the data task, rather than specifying a delegate
of nil
, like this:
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
delegate: nil
delegateQueue: [NSOperationQueue mainQueue]];
您应该使用不带委托的方法来实例化您的会话
:
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject];
或者,假设您根本没有自定义配置,则可以跳过实例化完全 NSURLSessionConfiguration
并使用共享的 NSURLSession
:
Or, alternatively, given that you're not customizing your configuration at all, you can skip the instantiation of the NSURLSessionConfiguration
altogether and use the shared NSURLSession
:
NSURLSession *defaultSession = [NSURLSession sharedSession];
但是,底线,你不应该使用 sessionWithConfiguration:delegate:queue :
rendition除非你要实现委托,在这种情况下你不会使用 NSURLSessionDataTask
方法的再现c $ c> completionHandler 参数。
But, bottom line, you should not use the sessionWithConfiguration:delegate:queue:
rendition unless you're going to implement the delegates, in which case you wouldn't use the rendition of the NSURLSessionDataTask
method with the completionHandler
parameter.
此外,请确保您的设备/模拟器运行iOS 7.0或更高版本。
Also, make sure your device/simulator is running iOS 7.0 or greater.
这篇关于NSURLSessionDataTask dataTaskWithURL完成处理程序未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!