iOS开发关于NSOperationQueue

发布于 2024-12-09 12:44:41 字数 424 浏览 0 评论 0原文

我知道获取操作队列的两种方法如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌枕肩 2024-12-16 12:44:41

根据文档 NSOperationQueue 的:

为非并发操作创建单独的线程并启动
当前线程的并发操作。

这解释了为什么有些任务在主线程上运行,而另一些则不在主线程上运行。

mainQueue 绑定到主线程,因此操作始终在主线程上执行。

According to the documentation NSOperationQueue's:

create separate threads for non-concurrent operations and launch
concurrent operations from the current thread.

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.

绅刃 2024-12-16 12:44:41

是的,斯蒂芬是对的。

主要目的是为非并发操作创建单独的线程,并从当前线程启动并发操作。

在这种情况下,

queue1 = [[NSOperationQueue alloc] init];

queue1是属于您调用的线程的队列,即,如果从分离的线程调用上面的行,那么它将不属于主线程。

但是

queue2 = [[NSOperationQueue mainQueue] retain];

您从 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

queue1 = [[NSOperationQueue alloc] init];

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

queue2 = [[NSOperationQueue mainQueue] retain];

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文