开发者

iOS develop about NSOperationQueue

开发者 https://www.devze.com 2023-04-12 22:53 出处:网络
I know the two ways to get a operation queue as follow: queue1 = [[NSOperationQueue alloc] init]; queue2 = [[NSOperationQueue mainQueue] retain];

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开发者_如何学Go 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. 


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号