我不确定如何使用 API performSelector:onThread
,在这里我需要一些建议。
据我所知,我需要一个runloop
来调用performSelector:onThread
,所以我做了一个。但是后来我发现了一个问题:一旦我打电话performSelector:onThread
,就runloop
停止了。
这是我的测试代码,runloop 在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];
}
我打电话kickOffThread
:performInBackground
[self performSelectorInBackground:@selector(kickOffThread) withObject:nil];
在 mainThread 我调用以下 API 将 threadCheck 设置为 YES,它被正确调用,但我的线程循环突然停止。
[self performSelector:@selector(signalThreadCheck) onThread:self.thread withObject:nil waitUntilDone:NO];
-(void)signalThreadCheck
{
NSLog(@" signalThreadCheck ... ");
threadCheck = YES;
}
终端日志结果如下,“thread exit ....”没有打印出来。有人告诉我问题出在哪里吗?
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 ...