-1

I'm new to IOS I need to implement NSThread in my program, but when it invoked it shows an SIGABRT error. my current code is given below

XMLParser.m

-(void)loadXML
{
    categories =[[NSMutableArray alloc]init];
    NSString *filepath =[[NSBundle mainBundle]pathForResource:@"cd_catalog" ofType:@"xml"];
    NSData *data =[NSData dataWithContentsOfFile:filepath];
    parser=[[NSXMLParser alloc]initWithData:data];
    parser.delegate =self;
    [parser parse];
}

ViewController.m

- (void)viewDidLoad
{
    xmlParser =[[XMLParser alloc]init];
    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(loadXML) object:nil];
    [myThread start];
    [super viewDidLoad];
}

please tell me what is wrong with my program

Optimus
  • 2,200
  • 8
  • 37
  • 71

4 Answers4

2

Use this code to solve your problem...

ViewController.m

- (void)viewDidLoad
{
    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(doParsing) object:nil];
    [myThread start];
    [super viewDidLoad];
}
-(void)doParsing
{
    xmlParser =[[XMLParser alloc]init];
    [xmlParser loadXML];
}
Bhanu Prakash
  • 1,493
  • 8
  • 20
0

Instead of creating a NSThread object you can start a thread using

//performSelectorInBackground:withObject: is NSObject's method
[self performSelectorInBackground:@selector(loadXML) withObject:nil];

I didn't find any buggy code but enable NSZombie and seee which object is causing this.

Community
  • 1
  • 1
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • It needs to be `[xmlParser performSelectorInBackground:@selector(loadXML) withObject:nil];` – rmaddy Mar 18 '13 at 05:35
0

loadXML is not define on ViewController so your thread code should be changed to use an instance of XMLParser instead of self like so:

XMLParser *parser = [[XMLParser alloc] init];
NSThread *thread = [[NSThread alloc] initWithTarget:parser selector:@selector(loadXML) object:nil];
[thread start];
Nick C
  • 645
  • 3
  • 6
0

Since Apple introduced GCD, you may solve it without creating any NSThread instance.

dispatch_async(dispatch_get_global_object(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  [self loadXML];
});
Daniyar
  • 2,975
  • 2
  • 26
  • 39