是否有相当于[NSOperationQueue currentQueue]或[NSThread currentThread]为NSOperation?
我有一个相当复杂的域模型,其中繁重的处理发生在调用堆栈中.为了及时取消操作,我需要将NSOperation参数作为参数传递给每个方法,直到我想要中断更长时间运行的循环.使用我可以使用的线程,[[NSThread currentThread] isCancelled]如果有一个NSOperation的等价物似乎很方便,不幸的是只有看似无用的东西[NSOperationQueue currentQueue].
我正在尝试编写一些线程安全的方法,所以我正在使用:
...
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_sync(main,^{
[self doSomethingInTheForeground];
});
...
Run Code Online (Sandbox Code Playgroud)
但是如果我在主线程上没有必要,我可以跳过所有这些调度调用,所以我想知道我目前正在使用什么线程.我怎么知道这个?
或者,也许它在性能方面没有差别?
可以做这个比较吗?
if (dispatch_get_main_queue() == dispatch_get_current_queue()){...}
Run Code Online (Sandbox Code Playgroud)