I've been working on this for several hours now and have become stumped. I'm probably doing something redundant or stupid that's causing this issue so any help I can get would be much appreciated.
I'm using NSURL to pull in a website: http://undignified.podbean.com/feed. From here I'm using NSXMLParser to parse the XML page and search for the elementName "enclosure". The "enclosure" element contains an attribute "url" which contains the URL for an Mp3. The parsing part works and I'm able to NSLog out the contents of the array and I've verified that all available URLs are present, but I cannot get this to load into a UITableView, it simply loads as blank. My array that I'm loading the parsed out attribute values in, _podAllEntries will Log to the console with the values within the dispatch_async, but when any other part of my ViewController accesses this array it is blank. I think since the async is being called on a background thread that maybe some sort of delegate has to be implemented so that other methods may access the values in this array? I may be completely wrong though.
To summarize I'm attempting to connect to an RSS feed (xml page), parse for an attribute called "url" and gather the value of it, and then load the URLs into a tableView. My code is listed below. Please feel free to comment if I'm unclear or ambiguous on anything.
UnDigParser.h - Class used to parse http://undignified.podbean.com/feed
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {
}
@property (retain) NSMutableArray *links;
@end
UnDigParser.h - implementation file:
#import "UnDigParser.h"
@implementation UnDigParser
NSMutableArray *_links;
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
_links=[[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"enclosure"]){
NSString *link = [attributeDict objectForKey:@"url"];
if (link){
[_links addObject:link];
}
}
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
ViewController.h file:
@interface getpodViewController : UITableViewController <UITableViewDataSource>{
NSMutableArray *_podAllEntries;
NSMutableArray *_allEntries;
}
@property(retain) NSMutableArray *podAllEntries;
@property(retain) NSMutableArray *allEntries;
@end
ViewController.M file:
#import "getpodViewController.h"
#import "PodcastEntry.h"
#import "UnDigParser.h"
@implementation getpodViewController
@synthesize podAllEntries = _podAllEntries;
@synthesize allEntries = _allEntries;
-(void)addRows{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed"];
UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];
[parser parse];
[_podAllEntries addObject:parser.links];
NSLog(@"%@", _podAllEntries);
});
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Podcasts";
self.podAllEntries = [[[NSMutableArray alloc]init]autorelease];
[self addRows];
for (UnDigParser *entry in _podAllEntries){
[_allEntries insertObject:entry atIndex:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_podAllEntries count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSString *cellValue = [_podAllEntries objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)dealloc {
[super dealloc];
[_podAllEntries release];
_podAllEntries = nil;
}
@end