检查当前线程是否为主线程
Objective-C 有没有办法检查当前线程是否是主线程?
我想做这样的事情。
- (void)someMethod
{
if (IS_THIS_MAIN_THREAD?) {
NSLog(@"ok. this is main thread.");
} else {
NSLog(@"don't call this method from other thread!");
}
}
Is there any way to check whether or not the current thread is the main thread in Objective-C?
I want to do something like this.
- (void)someMethod
{
if (IS_THIS_MAIN_THREAD?) {
NSLog(@"ok. this is main thread.");
} else {
NSLog(@"don't call this method from other thread!");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
查看
NSThread
API 文档 。有一些方法,例如
- (BOOL)isMainThread
+ (BOOL)isMainThread
和
+ (NSThread *)mainThread
Have a look at the
NSThread
API documentation.There are methods like
- (BOOL)isMainThread
+ (BOOL)isMainThread
and
+ (NSThread *)mainThread
在 Swift3 中
In Swift3
如果您希望某个方法在主线程上执行,您可以:
If you want a method to be executed on the main thread, you can:
如果你想知道你是否在主线程上,你可以简单地使用调试器。在您感兴趣的行处设置一个断点,当您的程序到达该断点时,调用:
(lldb) thread info
这将显示有关您所在线程的信息:
( lldb) 线程信息
线程#1:tid = 0xe8ad0,0x00000001083515a0 MyApp`MyApp.ViewController.sliderMoved(发件人= 0x00007fd221486340,self = 0x00007fd22161c1a0)(ObjectiveC.UISlider)-> () + 112 at ViewController.swift:20,队列 = 'com.apple.main-thread',停止原因 = 断点 2.1
如果
queue
的值为com.apple.main-thread
,那么您位于主线程上。If you want to know whether or not you're on the main thread, you can simply use the debugger. Set a breakpoint at the line you're interested in, and when your program reaches it, call this:
(lldb) thread info
This will display information about the thread you're on:
(lldb) thread info
thread #1: tid = 0xe8ad0, 0x00000001083515a0 MyApp`MyApp.ViewController.sliderMoved (sender=0x00007fd221486340, self=0x00007fd22161c1a0)(ObjectiveC.UISlider) -> () + 112 at ViewController.swift:20, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
If the value for
queue
iscom.apple.main-thread
, then you're on the main thread.以下模式将确保方法在主线程上执行:
The following pattern will assure a method is executed on the main thread:
请注意,我检查操作队列,而不是线程,因为这是一种更安全的方法
note that i check the operation queue, not the thread, as this is a more safer approach
两种方式。从@rano的回答来看,
另外,
Two ways. From @rano's answer,
Also,
对于 Monotouch / Xamarin iOS,您可以通过以下方式执行检查:
For Monotouch / Xamarin iOS you can perform the check in this way:
详细信息
解决方案 1. 检测任何队列
获取当前 DispatchQueue?
解决方案 2. 仅检测主队列
使用情况
结果示例
(日志)
Details
Solution 1. Detect any queue
Get current DispatchQueue?
Solution 2. Detect only main queue
Usage
Sample
Result (log)
迅捷版
Swift Version
从 https://stackoverflow.com/a/34685535/1530581 检查此答案
check this answer from https://stackoverflow.com/a/34685535/1530581
更新:似乎这不是正确的解决方案,根据@demosten提到的queue.h标头,
第一个想法出现在我身上,当我需要这个功能时,这行是:
并且已经查看了接受的解决方案:
我的解决方案快2.5倍。
PS 是的,我检查过,它适用于所有线程
UPDATE: seems that is not correct solution, according to queue.h header as mentioned @demosten
The first thought was brought to me, when I was needed this functionality was the line:
And had looked to the accepted solution:
mine solution 2.5 times faster.
PS And yes, I'd checked, it works for all threads