问题
I am new to NSoperation
. NSoperation
is a singleshot object.
How can we achieve multiple operations concurrently through
NSoperation
?is it possible without
NSoperationQueue
?Even if we use NSoperationQueue, it will perform operations FIFO format.How will it execute Concurrently?
回答1:
If you want to implement a concurrent operation—that is, one that runs asynchronously with respect to the calling thread—you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the start method starts the task and returns immediately and, in all likelihood, before the task is finished.
Most developers should never need to implement concurrent operation objects. If you always add your operations to an operation queue, you do not need to implement concurrent operations. 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. The ability to define concurrent operations is only necessary in cases where you need to execute the operation asynchronously without adding it to an operation queue.
See the Concurrency Programming Guide - section Concurrent Versus Non-concurrent Operations
Also Please read Managing Concurrency with NSOperation
You typically execute operations by adding them to an operation queue (an instance of the NSOperationQueue class).
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.
An operation queue executes its operations either directly, by running them on secondary threads, or indirectly using the libdispatch library
You can read more about NSOperation here and read more about NSOperationQueue here
来源:https://stackoverflow.com/questions/12910479/how-to-achieve-concurrent-task-through-nsoperation