diff options
Diffstat (limited to 'Classes/XMLParser.m')
-rw-r--r-- | Classes/XMLParser.m | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Classes/XMLParser.m b/Classes/XMLParser.m new file mode 100644 index 0000000..d39939e --- /dev/null +++ b/Classes/XMLParser.m @@ -0,0 +1,81 @@ +// +// XMLParser.m +// acidcow +// +// Created by Matthew Handler on 4/15/11. +// Copyright 2011 Earl Industries. All rights reserved. +// + +#import "XMLParser.h" +#import "Topic.h" + +static NSUInteger kCountForNotification = 10; + +@implementation XMLParser + +@synthesize delegate, parsedTopics; + + +- (void)start { + //self.startTimeReference = [NSDate timeIntervalSinceReferenceDate]; + [[NSURLCache sharedURLCache] removeAllCachedResponses]; + self.parsedTopics = [NSMutableArray array]; + NSURL *url = [NSURL URLWithString:@"http://feeds.feedburner.com/acidcow_com?format=xml"]; + [NSThread detachNewThreadSelector:@selector(downloadAndParse:) toTarget:self withObject:url]; +} + +- (void)dealloc { + [parsedTopics release]; + [super dealloc]; +} + +- (void)downloadAndParse:(NSURL *)url { + //NSAssert([self isMemberOfClass:[iTunesRSSParser class]] == NO, @"Object is of abstract base class iTunesRSSParser"); +} + +- (void)downloadStarted { + //NSAssert2([NSThread isMainThread], @"%s at line %d called on secondary thread", __FUNCTION__, __LINE__); + //self.downloadStartTimeReference = [NSDate timeIntervalSinceReferenceDate]; + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; +} + +- (void)downloadEnded { + //NSAssert2([NSThread isMainThread], @"%s at line %d called on secondary thread", __FUNCTION__, __LINE__); + //NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - self.downloadStartTimeReference; + //downloadDuration += duration; + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; +} + +- (void)parseEnded { + //NSAssert2([NSThread isMainThread], @"%s at line %d called on secondary thread", __FUNCTION__, __LINE__); + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(parser:didParseTopics:)] && [parsedTopics count] > 0) { + [self.delegate parser:self didParseTopics:parsedTopics]; + } + [self.parsedTopics removeAllObjects]; + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(parserDidEndParsingData:)]) { + [self.delegate parserDidEndParsingData:self]; + } + //NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - self.startTimeReference; + //totalDuration = duration; + //WriteStatisticToDatabase([[self class] parserType], downloadDuration, parseDuration, totalDuration); +} + +- (void)parsedTopic:(Topic *)topic { + //NSAssert2([NSThread isMainThread], @"%s at line %d called on secondary thread", __FUNCTION__, __LINE__); + [self.parsedTopics addObject:topic]; + if (self.parsedTopics.count > kCountForNotification) { + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(parser:didParseTopics:)]) { + [self.delegate parser:self didParseTopics:parsedTopics]; + } + [self.parsedTopics removeAllObjects]; + } +} + +- (void)parseError:(NSError *)error { + //NSAssert2([NSThread isMainThread], @"%s at line %d called on secondary thread", __FUNCTION__, __LINE__); + if (self.delegate != nil && [self.delegate respondsToSelector:@selector(parser:didFailWithError:)]) { + [self.delegate parser:self didFailWithError:error]; + } +} + +@end |