0

データをダウンロードするためにサーバーにリクエストを送信するには、NSOperationを使用しています。データを受信した後、NSXMLParserを使用して応答を解析していますが、パーサーデリゲートメソッドを呼び出していません。

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

また

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

誰かが私が間違っているところを教えてもらえますか?

//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];
}

注:MyXMLParserは、パーサーデリゲートメソッドを実装するNSObjectのサブクラスですが、カーソルがNSLogに到達していません。パーサーデリゲートメソッドにデバッグポイントを配置すると、これらのメソッドが呼び出されないことがわかりました。

誰かが問題がどこにあるのか、そして私がこれをどのように解決できるのかを教えてもらえますか?

前もって感謝します!

4

1 に答える 1

1

NSOperationのサブクラスを作成し、NSOperationのサブクラスのmainメソッドで次のように解析を実行することで、上記の問題を解決しました。

//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];
于 2012-07-24T10:45:49.320 に答える