How to achieve a parallel task through NSOperation?

I am new to NSoperation . NSoperation is an object of a single object.

  • How can we perform several operations simultaneously through NSoperation ?

  • maybe without an NSoperationQueue ?

  • Even if we use NSoperationQueue, it will perform operations on the FIFO file. How will it be performed at the same time?

+1
source share
1 answer

If you want to implement a parallel operation - that is, that runs asynchronously with respect to the calling thread - you must write additional code to start the operation asynchronously. For example, you can create a separate thread, call an asynchronous system function, or do something else to ensure that the start method starts the task and returns immediately and, most likely, before the task is completed.

Most developers should never create concurrent work objects. If you always add your operations to the operation queue, you do not need to perform concurrent operations. When you send a non-contact operation to the operation queue, the queue itself creates a thread on which your operation will be performed . Thus, adding a noncompetitive operation to the operation queue still leads to asynchronous execution of the operation object code. The ability to define parallel operations is necessary only in those cases when you need to perform an operation asynchronously without adding it to the operation queue.

See Concurrency Programming Guide - Parallel and Non-Competitive Operations

Also read Concurrency Management with NSOperation

Typically, operations are performed by adding them to the operation queue. (instance of the NSOperationQueue class).

The NSOperationQueue class controls the execution of a set of NSOperation objects. After adding to the queue, the operation remains in this queue until it is explicitly canceled or completes its task. Operations in the queue (but not yet performed) are themselves organized in accordance with the priority levels and dependencies between operational objects and are performed accordingly . An application can create multiple queues of operations and send operations to any of them.

Operational queue performs its operations either directly, running them on secondary threads, or indirectly using the libdispatch library

Read more about NSOperation here and more about NSOperationQueue here.

+2
source

Source: https://habr.com/ru/post/1439973/


All Articles