0

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!

Rob
  • 4,927
  • 12
  • 49
  • 54
Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
  • Is `createRequestToGetData` being called at all? Do you have breakpoints in there? – jrturton Jul 24 '12 at 09:40
  • Please include the code of your MyXMLParser class. Does `parseXMLFileWithData:parseError:` create a NSXMLParser, set itself up as a delegate, and call the parsing method of the NSXMLParser? – Thomas Deniau Jul 24 '12 at 09:42
  • Yes createRequestToGetData is getting called and MyXMLParser set itself as NSXMLParser delegate. – Nuzhat Zari Jul 24 '12 at 10:38

1 Answers1

1

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];
Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36