4

That is, if we queue the same thing several time there will be no concurrency. The one we queued first will be executed first.

I mean there is only one main thread right?

B.S.
  • 21,660
  • 14
  • 87
  • 109
user4951
  • 32,206
  • 53
  • 172
  • 282

3 Answers3

6

I have found a nice answer here:

NSOperationQueue and concurrent vs non-concurrent

So make all added operations serial you can always set:

[[NSOperationQueue mainQueue] setMaxConcurrentOperationCount:1];

Community
  • 1
  • 1
B.S.
  • 21,660
  • 14
  • 87
  • 109
2

And the answer is... YES and NO

when you create a new NSOperation to add to your queue, you can use

- (void)setQueuePriority:(NSOperationQueuePriority)priority

according to the documentation, the queue will use this priority, and other factors as inter dependency to decide what operation will be executed next.

As long as your operations have the same priority and no inter-operation dependencies, they should be executed in the same order you added them, maybe with other, system related operations, inserted between them.

Jerome Diaz
  • 1,746
  • 8
  • 15
  • So If I set my operations's queuePriority to very low and queue it on main queue, does that guarantee my operation will always be executed after all the system-relataed operations ? – Luong Huy Duc Jan 25 '14 at 08:15
1

From documentation:

The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

Inter-operation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. An operation object is not considered ready to execute until all of its dependent operations have finished executing. For operations that are ready to execute, the operation queue always executes the one with the highest priority relative to the other ready operations. For details on how to set priority levels and dependencies, see NSOperation Class Reference.

About threads:

Although you typically execute operations by adding them to an operation queue, doing so is not required. It is also possible to execute an operation object manually by calling its start method, but doing so does not guarantee that the operation runs concurrently with the rest of your code. The isConcurrent method of the NSOperation class tells you whether an operation runs synchronously or asynchronously with respect to the thread in which its start method was called. By default, this method returns NO, which means the operation runs synchronously in the calling thread.

When you submit a nonconcurrent operation to an operation queue, the queue itself creates a thread on which to run your operation. Thus, adding a nonconcurrent operation to an operation queue still results in the asynchronous execution of your operation object code.

So, if I understand correctly here will be no concurrency.

Community
  • 1
  • 1
Artem Shmatkov
  • 1,434
  • 5
  • 22
  • 41
  • mainQueue is different than just another NSOperationQueue. For example, maximum concurent operation is 1 – user4951 Apr 04 '13 at 08:38