PerformSelector NSThread 问题
在performSelector waitsUntilDone 时是否可以取消线程?
请参见下文:
我有一个 while 循环,其中包含以下行:
[self performSelector:@selector(getMyRecords:) onThread:myThread withObject:i waitUntilDone:YES]
有时,我需要在循环执行时取消线程 myThread
。我遇到以下崩溃问题:
Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[MyController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'
Is it possible to cancel a thread while performSelector waitsUntilDone?
See below:
I have a while loop with following line:
[self performSelector:@selector(getMyRecords:) onThread:myThread withObject:i waitUntilDone:YES]
Sometimes, I need to cancel my thread myThread
while my loop executes. I get following crash issue:
Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[MyController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果线程内有一个长时间运行的操作,您可以声明一个条件变量(原子),例如“取消”并检查它。
像这样的东西:
if you have a long running operation inside a thread, you can declare a condition variable (atomic) like 'canceled' and check it.
something like:
我认为最简单的方法是使用
NSThread's
-(void)取消方法。I think the easiest way is to use
NSThread's
-(void)cancel method.查看NSObject的文档:
Check out the documentation of NSObject: