我基本上使用this教程来制作RSS阅读器。我认为这个不是多线程的。
我还看了一下应该是多线程的raywenderlich's tutorial,但我不能使用它,因为它已经过时了,而且库不再起作用了。
第一个教程非常快速地加载内容。但我尝试在网页浏览中加载它。所以我有5个页面,每个页面加载不同的RSS。
在教程中(在我的代码中也是这样),解析发生在ViewDidLoad:
中- (void)viewDidLoad
{
[super viewDidLoad];
//initialize
images = [[NSMutableArray alloc] init];
feeds = [[NSMutableArray alloc] init];
currentRssUrl = [NSURL URLWithString:self.rssUrl];
parser = [[NSXMLParser alloc] initWithContentsOfURL:currentRssUrl];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
现在开始我并不完全理解代码,但我认为[parser parse]会进行解析。
这是我的其余代码。
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.numberOfLines = 2;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
NSString *yt = @"videos";
NSString *videoCheck = [currentRssUrl absoluteString];
if(images.count > 0)
{
NSURL *imgUrl = [NSURL URLWithString:images[indexPath.row]];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgUrl];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
}
else if ([videoCheck rangeOfString:yt].location != NSNotFound)
{
NSString *ytLink = [feeds[indexPath.row] objectForKey: @"link"];
NSArray *stringArray = [ytLink componentsSeparatedByString:@"/"];
NSString *ytId = stringArray[stringArray.count-1];
ytId = [ytId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *ytThumbUrl = [NSString stringWithFormat:@"http://img.youtube.com/vi/%@/default.jpg", ytId];
NSURL *imgUrl = [NSURL URLWithString:ytThumbUrl];
NSData *data = [[NSData alloc] initWithContentsOfURL:imgUrl];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
}
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
element = elementName;
if ([element isEqualToString:@"item"])
{
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
enclosure = [[NSMutableString alloc] init];
}
if(![elementName isEqual:@"enclosure"])
return;
NSString *urlTitle = @"app_thumb";
//als de title gelijk is aan app_thumb
if([[attributeDict objectForKey:@"title"] isEqual:urlTitle])
{
//lees de url attribute uit
NSString * name = [attributeDict objectForKey:@"url"];
// voeg de url toe aan images
[images addObject:name];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"item"])
{
[item setObject:title forKey:@"title"];
[item setObject:link forKey:@"link"];
[item setObject:enclosure forKey:@"enclosure"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"])
{
[title appendString:string];
}
else if ([element isEqualToString:@"link"])
{
[link appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[self.tableView reloadData];
}
那么如何在后台线程中进行解析呢?还有什么时候最好加载。在我的Android版本中,当页面变得可见时,我启动了AsyncTask。
我觉得这很难,但就这个问题而言。我想知道如何在后台线程中进行解析,任何额外的多线程技巧都会受到赞赏。
答案 0 :(得分:1)
尝试使用GCD:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Background running Code
/*
//initialize
images = [[NSMutableArray alloc] init];
feeds = [[NSMutableArray alloc] init];
currentRssUrl = [NSURL URLWithString:self.rssUrl];
parser = [[NSXMLParser alloc] initWithContentsOfURL:currentRssUrl];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
*/
});
然后在主线程中更新结果:
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
dispatch_sync(dispatch_get_main_queue(), ^{
// [self.tableView reloadData];
});
}