Nuzhat Zari
Nuzhat Zari

Reputation: 3408

NSXMLParser along with NSOperation is not working properly

To send request to server to download data I am using NSOperation.After receiving data I am using NSXMLParser to parse response but it is not calling parser delegate methods such

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

or

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

Can anyone tell where I am doing wrong.

//Creating NSOperation as follows:
NSOperationQueue *operationQueue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(createRequestToGetData) object:nil]; 
[operationQueue addOperation:operation];
[operation release];    

-(void)createRequestToGetData
{
    NSError *error;
    NSURL *theURL = [NSURL URLWithString:myUrl];
    NSData *data = [NSData dataWithContentsOfURL:theURL];

    MyXMLParser *theXMLParser = [[MyXMLParser alloc]init];
    NSError *theError = NULL;
    [theXMLParser parseXMLFileWithData:data parseError:&theError];
    NSLog(@"Parse data:%@",theXMLParser.mParsedDict); //Cursor is not coming here.
    [theXMLParser release];
}

Note: MyXMLParser is subclass of NSObject which implement Parser delegates methods but my cursor is not reaching at NSLog.When placed debug point in Parser delegate methods found that those methods are not getting called.

Can anyone tell where is the problem and how I can resolve this.

Thanks in advance!

Upvotes: 0

Views: 428

Answers (1)

Nuzhat Zari
Nuzhat Zari

Reputation: 3408

I solved above problem by creating subclass of NSOperation and performed parsing in main method of subclass of NSOperation as follows:

//DataDownloadOperation.h
#import <Foundation/Foundation.h>

@interface DataDownloadOperation : NSOperation
{
    NSURL *targetURL;
}
@property(retain) NSURL *targetURL;
- (id)initWithURL:(NSURL*)url;

@end


//DataDownloadOperation.m
#import "DataDownloadOperation.h"
#import "MyXMLParser.h"

@implementation DataDownloadOperation
@synthesize targetURL;

- (id)initWithURL:(NSURL*)url
{
    if (![super init]) return nil;
    self.targetURL = url;
    return self;
}

- (void)dealloc {
    [targetURL release], targetURL = nil;
    [super dealloc];
}

- (void)main {

    NSData *data = [NSData dataWithContentsOfURL:self.targetURL];
    MyXMLParser *theXMLParser = [[MyXMLParser alloc]init];
    NSError *theError = NULL;
    [theXMLParser parseXMLFileWithData:data parseError:&theError];
    NSLog(@"Parse data1111:%@",theXMLParser.mParsedDict);
    [theXMLParser release];
}
@end
//Created NSOperation as follows:
NSURL *theURL = [NSURL URLWithString:myurl];
        NSOperationQueue *operationQueue = [NSOperationQueue new];
        DataDownloadOperation *operation = [[DataDownloadOperation alloc] initWithURL:theURL];
         [operationQueue addOperation:operation];
         [operation release];

Upvotes: 1

Related Questions