I am using NSXmlParser to parse through an rss feed. All is working well so far.
I'm anticipating the rss feed will eventually contains dozens/hundreds of posts. My current solution is reading the entire rss feed and displaying the results. However I want to only read the first ten posts (to prevent it parsing potentially hundreds of items). Then at a later time (say when the user reaches the end of the table) to parse the next ten posts.
So my question is how would I parse the first ten posts, then parse the next ten posts, then the next ten posts and so on...
Here is what I am using to get ALL posts:
- (void)parseXMLFileAtURL:(NSString *)URL
{
myArray = [[NSMutableArray alloc] init];
//convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
//error
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
//clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// save the characters for the current item...
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[myTable reloadData];
}
dispatch_async
. If you're not familiar with GCD, I suggest watching the WWDC videos on the matter and prepare to get your mind blown. – Addict