作为标题
NSData *data = [NSData dataWithContentsOfURL:URL];
现在我想用NSURLSessionDataTask
替换它。但是我有一个问题,就是NSURLSessionDataTask没有成功返回数据
我试图打破断点并发现它在主线程中运行,但它没有运行。
//1 on main thread
NSURLSessionDataTask *loadDataTask = [session dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// don't work
dataU = data;
TFHpple *hpple = [[TFHpple alloc] initWithHTMLData:dataU];initWithHTMLData:dataU];
NSString *pathQueryString = @"//div[@class='newslist l']/ul/li/a"; //
NSArray *Nodes = [hpple searchWithXPathQuery:pathQueryString];
for (TFHppleElement *element in Nodes) {
MKNewsItem *newsItem = [[MKNewsItem alloc] init];
// 6
newsItem.title = [element text];
newsItem.time = [[element firstChildWithTagName:@"span"] content];
NSMutableString *tempTime = [NSMutableString stringWithFormat:@"%@", newsItem.time];
[tempTime replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
[tempTime replaceCharactersInRange:NSMakeRange(10, 1) withString:@""];
newsItem.time = tempTime;
newsItem.url = [NSURL URLWithString:[element objectForKey:@"href"]];
NSMutableString *tempUrl = [NSMutableString stringWithFormat:@"http://www.ayit.edu.cn%@",newsItem.url.absoluteString];
[tempUrl deleteCharactersInRange:NSMakeRange(22, 2)]; //删除那两个..
newsItem.url = [NSURL URLWithString:tempUrl];
[newsGroup addObject:newsItem];
}
_news = nil;
_news = newsGroup;
}];
//2
[loadDataTask resume];
实施NSURLSessionDataTask