summaryrefslogtreecommitdiffstats
path: root/Classes/XMLParser.m
blob: d39939eb7435906ca3bc09a8c2f3825139a2366e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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