// // 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