wangdg
wangdg

Reputation: 1

iOS develop about NSOperationQueue

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. 

Upvotes: 0

Views: 826

Answers (2)

RamaKrishna Chunduri
RamaKrishna Chunduri

Reputation: 1130

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)

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52575

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.

Upvotes: 1

Related Questions