Voloda2
Voloda2

Reputation: 12607

My NSXMLParser doesn't work in background

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.

Upvotes: 3

Views: 818

Answers (1)

Joel Kravets
Joel Kravets

Reputation: 2471

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

Upvotes: 4

Related Questions