iOS开发关于NSOperationQueue
我知道获取操作队列的两种方法如下:
queue1 = [[NSOperationQueue alloc] init];
queue2 = [[NSOperationQueue mainQueue] retain];
但我不知道它们之间有什么区别。
[queue1 addOperation:operation1];
[queue2 addOperation:operation2];
操作1在哪个线程上运行?主线程?或不确定性?
我测试过。
operation1 --> sometimes mainthread sometimes not.
operation2 --> always mainthread.
I know the two ways to get a operation queue as follow:
queue1 = [[NSOperationQueue alloc] init];
queue2 = [[NSOperationQueue mainQueue] retain];
But I don't know what differences between them.
[queue1 addOperation:operation1];
[queue2 addOperation:operation2];
which thread does operation1 run on?main thread? or uncertainty?
I tested.
operation1 --> sometimes mainthread sometimes not.
operation2 --> always mainthread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档 NSOperationQueue 的:
这解释了为什么有些任务在主线程上运行,而另一些则不在主线程上运行。
mainQueue
绑定到主线程,因此操作始终在主线程上执行。According to the documentation NSOperationQueue's:
This explains why some of your task operate on the main thread and others don't.
The
mainQueue
is bound to the main thread, so operations are always performed on the main thread.是的,斯蒂芬是对的。
主要目的是为非并发操作创建单独的线程,并从当前线程启动并发操作。
在这种情况下,
queue1是属于您调用的线程的队列,即,如果从分离的线程调用上面的行,那么它将不属于主线程。
但是
您从 ios 外部获取队列,换句话说,第一个是调用的 VC/类的本地队列,第二个是全局的(ios 4 中一对一的应用程序)
Yep, Stephen is correct.
The main purpose is to Create separate threads for non-concurrent operations and launch concurrent operations from the current thread.
In this case
the queue1 is the queue which belongs to the thread from which you have invoked i.e., if the above line is invoked from a detached thread then it would not belong to main thread.
but with
You are externally getting the queue from the ios in other words 1st is local to the VC/Class called and second one is global (one for one application in ios 4)