3

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.

Voloda2
  • 12,359
  • 18
  • 80
  • 130

1 Answers1

4

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?

- (void)startParsing{
//...

  [NSThread detachNewThreadSelector:@selector(parseXML:) 
                   toTarget:self withObject:parseData];

//..
}

- (void)parseXML:(id)parseData
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSXMLParser * parser = [[NSXMLParser alloc] initWithData:parseData];
  parser.delegate = self;
  [parser parse];
  [parser release];
  [pool drain];
}
Joel Kravets
  • 2,473
  • 19
  • 16
  • Yes, it will open 4 new threads each with it's own parser object. As long as your delegate can handle 4 separate parsers then it should be okay. – Joel Kravets Feb 09 '12 at 15:44