performSelector:onThread会破坏运行循环吗?
问题说明
我不确定如何使用API performSelector:onThread
,在这里我需要一些建议.
据我所知,我需要一个runloop
来调用performSelector:onThread
,所以我做了一个.但是随后我发现了一个问题:呼叫performSelector:onThread
后,runloop
就会停止.
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.
这是我的测试代码,runloop在function kickOffThread
中.
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];
}
我通过performInBackground
呼叫kickOffThread
:
[self performSelectorInBackground:@selector(kickOffThread) withObject:nil];
在mainThread中,我调用以下API将threadCheck设置为YES,它被正确调用,但是我的线程循环突然停止.
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 ...
正确答案
线程未中断. 它只是卡在了mach_msg_trap
the thread is not breaked. it just stucked in mach_msg_trap
如果没有输入源或计时器附加到运行循环,则此方法立即退出;否则,此方法将退出.否则,它将在NSDefaultRunLoopMode中运行接收器
If no input sources or timers are attached to the run loop, this method exits immediately; otherwise, it runs the receiver in the NSDefaultRunLoopMode
perfromSelector:onThread导致调用方将输入源添加到目标线程. 因此[NSRunLoop run]方法实际上将调用[NSRunLoop(NSRunLoop)runMode:beforeDate:] 并发送一个无限的日期作为beforeDate参数.
the perfromSelector:onThread cause the caller add a input source to the target thread. so the method [NSRunLoop run] will actually call [NSRunLoop(NSRunLoop) runMode:beforeDate:] and send a infinite date as beforeDate param.
如果要停止线程,请改用[NSRunLoop(NSRunLoop)runMode:beforeDate:],并发送一个有限的日期,例如:
if you want to stop the thread, use [NSRunLoop(NSRunLoop) runMode:beforeDate:] instead, and send a limited date, like this:
BOOL shouldKeepRunning = YES; // global
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning &&
[theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhacj
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20