我的 NSXMLParser 无法在后台工作
解析效果很好。
-(void) callParse
{
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
[parser release];
}
我想在后台执行解析。这段代码不做任何解析。但为什么?
@interface NSXMLParser(Private)
- (void)myParse;
@end
@implementation NSXMLParser(Private)
- (void)myParse
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self parse];
[pool drain];
}
@end
-(void) callParse2
{
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[NSThread detachNewThreadSelector:@selector(myParse) toTarget:parser withObject:nil];
[parser release];
}
更新:我调用 callParse2 4 次,它创建了 4 个线程。它做了一些解析,但结果很混乱。可能我对同步变量有一些问题。 NSXMLParser 调用使用非原子属性的委托。
Parsing works great.
-(void) callParse
{
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
[parser release];
}
I want to perform parsing in background. This code doesn't do any parsing. But why?
@interface NSXMLParser(Private)
- (void)myParse;
@end
@implementation NSXMLParser(Private)
- (void)myParse
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self parse];
[pool drain];
}
@end
-(void) callParse2
{
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[NSThread detachNewThreadSelector:@selector(myParse) toTarget:parser withObject:nil];
[parser release];
}
UPDATE: I call callParse2 4 times and it creates 4 threads. It does some parsing but the results is messy. May be I have some problem with synchronization variables. NSXMLParser calls delegates which uses nonatomic properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定为什么它在类别方法中不起作用,但是您是否尝试过激活正在调用 NSXMLParser 的对象上的线程?
I'm not entirely sure why it wouldn't work in a category method but have you tried activating the thread on the object your are calling the NSXMLParser from?