gpt4 book ai didi

ios - 如何在后台线程中解析?

转载 作者:行者123 更新时间:2023-11-29 02:58:13 25 4
gpt4 key购买 nike

我基本上用了this制作 RSS 阅读器的教程。我认为这个不是多线程的。

我还看了raywenderlich's tutorial它应该是多线程的,但我不能使用它,因为它已经过时并且库不再工作。

第一个教程加载内容的速度非常快。但我尝试将其加载到页面 View 中。所以我有 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。

我发现这很难,但就这个问题而言。我想知道如何在后台线程中进行解析,任何额外的多线程技巧都会受到赞赏。

最佳答案

尝试使用 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];

});

}

Here is the good tutorial .

关于ios - 如何在后台线程中解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com