Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a function using thread in xcode

i have created a thread in xcode and i have given the function name to be called from that thread. but my problem is that the function name which is given to call is not being called(came to know when put a breakpoint in that function)

code:

 NSThread* myThread; 
 [myThread start]; 
 [self performSelector:@selector(func1:) onThread:myThread withObject:nil waitUntilDone:false]

and later i tried this one also:

NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(func1:)object:nil];
[myThread start]; 

above func1 is the name of the function to be called.

so can any one please tell me how to create the thread and call func1 from there....

like image 930
Subrat nayak. Avatar asked May 03 '12 07:05

Subrat nayak.


1 Answers

In your first code sample it doesn't look like you are actually creating a new thread. You create an empty myThread variable and then call start on it but this will just result in start being sent to nil. The empty thread variable is then sent to the performSelector:onThread:withObject:waitUntilDone: method which will presumably do nothing.

You will need to properly create a thread before you can actually run something on it using performSelector:onThread:withObject:waitUntilDone:.

Alternatively, it would be much easier, assuming you don't care which background thread the method runs on, to simply use performSelectorInBackground:withObject:. For example:

[self performSelectorInBackground:@selector(func1:) withObject:nil];
like image 107
mttrb Avatar answered Sep 22 '22 21:09

mttrb