I'm not sure how to use the API performSelector:onThread and I need some suggestions here.
As far as I known, I need a runloop to make the call of performSelector:onThread, so I made one. But then I find a problem : once I called performSelector:onThread, the runloop stops.
Here 's my test code, runloop is in function kickOffThread.
- (void)setCurrentThread
{
self.thread = [NSThread currentThread];
}
- (void)kickOffThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSelector:@selector(setCurrentThread) withObject:nil afterDelay:0];
while (!threadCheck) {
NSLog(@"threadCheck = %d", threadCheck);
[[NSRunLoop currentRunLoop] run];
}
[self doSomeCleanUp];
NSLog(@"thread exit ....");
[pool release];
}
I call kickOffThread by performInBackground:
[self performSelectorInBackground:@selector(kickOffThread) withObject:nil];
In mainThread I call following API to set threadCheck to be YES, it is correctly called, but my thread loop suddenly stops.
[self performSelector:@selector(signalThreadCheck) onThread:self.thread withObject:nil waitUntilDone:NO];
-(void)signalThreadCheck
{
NSLog(@" signalThreadCheck ... ");
threadCheck = YES;
}
terminal log result is as follows, "thread exit ...." not printed. Anyone tells me where is the problem ?
2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.827 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.836 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.837 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.840 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.844 MBSMapSample[23582:17403] threadCheck = 0
2013-06-07 15:51:54.846 MBSMapSample[23582:17403] signalThreadCheck ...