Gerald
Gerald

Reputation: 677

Call a function that will start a NSThread on a pointer to function

I'm trying to do something in ObjectiveC (which is far from being a language that I know) and I'm not sure it's possible.

I have an interface QAFeatTestCaseBase, with a function that will create a thread and start a function in it, and wait for it to finish :

- (void)callInBackground:(id)target selector:(SEL)selector
{  
   NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(selector) object:nil];
   [thread setStackSize: 2*1024*1024];
   [thread start];

   while([thread isFinished] == NO) { usleep(1000); }
   [thread release];
} 

And then I inherite from it to implement a function for a specific test :

@interface Bug8772 : QAFeatTestCaseBase
- (void) testBug8772
{
   [self callInBackground:self selector:@selector(Bug8772)];
}

- (void)Bug8772
{
    // do stuff..
}

But I have an error : caught NSInvalidArgumentException [NSThread initWithTarget:selector:object:]: target does not implement selector (*** - [Bug8722 selector])

Can you point me what I'm doing wrong? Or if there is a better way to do this?

Upvotes: 0

Views: 129

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

selector is already a selector. Don't wrap it with @selector.

- (void)callInBackground:(id)target selector:(SEL)selector
{  
    NSThread* thread = [[NSThread alloc] initWithTarget:self selector:selector object:nil];
   [thread setStackSize: 2*1024*1024];
   [thread start];

   while([thread isFinished] == NO) { usleep(1000); }
   [thread release];
}

Note, NSThread is hard to get working correctly. I don't think the above code will do what you want.

For a better answer, look into Dispatch. Dispatch is also called GCD or Grand Central Dispatch.

Upvotes: 1

Related Questions