3
votes

I am using NSInvocationOperation with NSOperationQueue developing in iOS5. According to apple documentation on invocation objects:

The NSInvocationOperation class is a concrete subclass of NSOperation... This class implements a non-concurrent operation.

Ok, so my NSInvocationOperation object executes synchronously? (correct me here) Apple's docs also says on operation queue objects:

In iOS, operation queues do not use Grand Central Dispatch to execute operations. They create separate threads for non-concurrent operations and launch concurrent operations from the current thread.

I am using the NSInvocationObject, which is a non-concurrent object, and adding it to to the operation queue like so:

[operationQueue addOperation:operation];

so my question is: 1) Since the operation queue will be spawning a separate thread to execute the NSInvocationObject as it says in the docs, will it be run asynchronously instead of synchronously?

[UPDATE - in response to Dani's answer below. Taken from an Apple link.

NSOperation and NSOperationQueue

There are a number of different ways that you can use NSOperation, but the most common is to write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run. NSOperation classes written in this way are known as non-concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don't have to handle the concurrency yourself.)

If you need more control over threading and the run-time environment of your operations, you can make use of concurrent operations. To do this, you subclass NSOperation and override the start method. In the start method, you can spawn threads and setup the environment before calling the main method. You are also required to maintain the state of the NSOperation by setting properties like isExecuting and isFinished. In short, concurrent operations give you a lot more control, but also demand more effort—for most tasks non-concurrent operations suffice.

1
Concurrent in the sense of the "isConcurrent" flag on NSOperations is a curious and somewhat misleading term. It is used consistently and vaguely makes sense in the context, but it may not mean what you expect.Catfish_Man

1 Answers

9
votes

If you add it to an operation queue it will run asynchronously with respect to the rest of your code, but synchronously on the operation queue. It's pretty much like:

With synchronous objects:

Creating thread
    |
    |\
    | \______
    |        |
    |       Operation A
    |        |
    |       Operation B
    |        |
    |        |

With asynchronous objects:

Creating thread
    |
    |\
    | \___________________
    |                     |
    |\                  Operation A
    | \______             |
    |        |            |
    |       Operation B   |
    |        |            |
    |        |            |