Reputation: 611
I basically used this tutorial to make a RSS reader. I think this one is not multithreaded.
I also took a look at raywenderlich's tutorial which should be multithreaded, but I couldn't use this because it is out of date and the library doesn't work anymore.
The first tutorial loads the content pretty fast. But I try to load it in a pageview. So I have 5 pages each loading a different RSS.
In the tutorial (and so in my code) parsing happens in 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];
}
Now it start with me not completely understanding the code but I think [parser parse] does the parsing.
here is the rest of my code.
#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];
}
So how can I make the parsing in a background thread. Also when is it best to load. In my android version I started a AsyncTask when the page became visible.
I find this very hard but as far as this question goes. I want to know how to do the parsing in a background thread, any extra multithreading tips are appreciated.
Upvotes: 0
Views: 399
Reputation: 15335
Try with the 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];
*/
});
Then update the result in the Main Thread:
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
dispatch_sync(dispatch_get_main_queue(), ^{
// [self.tableView reloadData];
});
}
Upvotes: 1