I am trying to download some XML on another thread, and parse it.
I release the 'controller' then call cancelAllOperations on the NSOperationQueue. And implement method 'cancel' on NSoperation which attempts to set nSXMLParser's delegate to nil.
But a second or so later the NSXMLParser is still alive and kicking and calls methods on it's delegate (which now no longer exists) causing a crash.
I just dont get it, what am I doing wrong?
#import "LoadXMLTheadedController.h"
#import "LoadXMLThreaded.h"
@implementation LoadXMLTheadedController
- (id)initWithURLString:(NSString *)newString
{self = [super init];
queue = [[NSOperationQueue alloc] init];
loadXMLThreaded = [[LoadXMLThreaded alloc] initWithDelegate:self andXMLURLString:newString];
[queue addOperation:loadXMLThreaded];
return self;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@" do some parsing.. ");
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[queue cancelAllOperations];
[loadXMLThreaded release];
[queue release];
[super dealloc];
}
@end
//----------------------------------------------------------------//
#import "LoadXMLThreaded.h"
@implementation LoadXMLThreaded
- (id)initWithDelegate:(id)newDelegate andXMLURLString:(NSString *)newString
{
[super init];
delegate = newDelegate;
url = [[NSURL URLWithString:newString]retain];
return self;
}
- (void)cancel{
nSXMLParser.delegate = nil;
[nSXMLParser abortParsing];
}
- (void)main {
nSXMLParser = [[NSXMLParser alloc]initWithContentsOfURL:url];
nSXMLParser.delegate = delegate;
[nSXMLParser parse];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
nSXMLParser.delegate = nil;
[nSXMLParser abortParsing];
[nSXMLParser release];
[url release];
[super dealloc];
}
@end