Fred Novack
Fred Novack

Reputation: 811

NSThread doesn't execute selector

I have a static object that fires a thread, but whenever the thread tries to execute the selector i get a "[NSThread initWithTarget:selector:object:]: target does not implement selector" and the app crashes

heres my code:

@implementation currentUser
{
    NSThread *engineThread;
}

-(void)MessageEngineStart{
    NSLog(@"[MDS]:Message Engine Started!");    
    if(engineThread == nil){
        engineThread = [[NSThread alloc]init];
    }

    if(!engineThread.isExecuting){
        [engineThread performSelectorInBackground:@selector(job) withObject:nil];//here is where it crashes
        NSLog(@"[MDS]: Thread Will perform job in background.");
    }
    else{
        NSLog(@"[MDS]: Thread is being executed.");
    }
    [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(MessageEngineStart) userInfo:nil repeats:NO];
}
-(void)job
{
   //JOB
}

both thread and job are on the same object. if I just use a

[NSThread detachNewThreadSelector:@selector(job) toTarget:self withObject:nil];

everything goes well...

What am I doing wrong?

Upvotes: 0

Views: 504

Answers (1)

Wain
Wain

Reputation: 119031

You don't need to create a thread for this, you should just be calling

[self performSelectorInBackground:@selector(job) withObject:nil];

because it is your class which implements job and not the NSThread class. When you call

[NSThread detachNewThreadSelector:@selector(job) toTarget:self withObject:nil];

your are using NSThread to call job on self so it works.

Upvotes: 3

Related Questions