开发者

Multiple feeds with MWFeedParser, help with code

开发者 https://www.devze.com 2023-04-03 05:37 出处:网络
I currently have this code but it leaks because I don\'t know how to release feed parser object this many times, so I need help with memory management or some other way to feed multiple feeds with it.

I currently have this code but it leaks because I don't know how to release feed parser object this many times, so I need help with memory management or some other way to feed multiple feeds with it.

Code:

for (NSString *imePredmeta in [Data variables].mojiPredmeti) {
        NSString *link = [[Data variables].rss objectForKey: imePredmeta];
        NSURL *feedURL = [NSURL URLWithString: link];
        feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
        feedParser.delegate = self;
        feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items
        feedParser.connectionType = ConnectionTypeAsynchronously;
        [feedParser parse];        
    }

Dealloc:

- (void)dealloc {
    [formatter release];
    [parsedItems release];
    [itemsToDisplay release];
    [feedParser release];
    [super dealloc];
}

And initWithFeedUrl from MWFeedParser code is:

- (id)initWithFeedURL:(NSURL *)feedURL {
    if ((self = [self init])) {

        // Check if an string was passed as old init asked for NSString not NSURL
        if ([feedURL isKindOfClass:[NSString class]]) {
            feedURL = [NSURL URLWithString:(N开发者_StackOverflowSString *)feedURL];
        }

        // Remember url
        self.url = feedURL;

    }
    return self;
}


The best way would be to add them to an array, and then in dealloc release that array.

// declare an iVar in your header file
NSMutableArray *feeds;

// instantiate the array, but release it first as you may call your method more 
// than once
if (feeds) {
    [feeds release];
}
feeds = [[NSMutableArray alloc] init];
for (NSString *imePredmeta in [Data variables].mojiPredmeti) {
    ...
    // after calling parse on the feed, add it to the array and then release it. 
    // Objects added to an array are retained by the array so this is safe
    [feedParser parse];
    [feeds addObject:feedParser]; // <-- Add parser to array
    [feedParser release]; // <-- Release parser 
}

// then in your dealloc simply release the array, which will in-turn release all 
// the parser objects it contains
- (void)dealloc {
    [formatter release];
    [parsedItems release];
    [itemsToDisplay release];
    [feeds release]; // <-- Release feeds array
    // I've removed the call to release feedParser as it's already been released
    [super dealloc];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消