What is isConcurrent for NSOperation launched from NSOperationQueue?

Since NSOperationQueue always runs tasks in a new thread, I got confused in the role of isConcurrent when NSOperation runs on NSOperationQueue .

If I have two subclasses of NSOperation , both start asynchronous processes, both start from NSOperationQueue , and in both cases I override isCancelled , isExecuting , isFinished and isReady . What difference isConcurrent it make if on one I always override isConcurrent always return YES , and the other always on return NO .

Who is actually calling isConcurrent ? How logic changes if it is NO or YES .

+6
source share
3 answers

This is an obsolete method used before OS X v10.6 and before iOS 4, i.e. before introducing the GCD to send the NSOperationQueue .

From doc

Operational queues typically provide threads used to perform their operations. In OS X v10.6 and later, operational queues use the libdispatch library (also known as Grand Central Dispatch) to initiate their operations. As a result, operations are always performed in a separate thread , regardless of whether they are assigned as parallel or non-competitive operations. In OS X v10.5, however, operations are performed on separate threads only if their isConcurrent method returns NO . If this method returns YES, it is expected that the work object will create its own thread (or start some asynchronous operation); the queue does not provide a thread for it.

If you use OS X> = 10.6 or iOS> = 4, you can safely ignore it.

As a confirmation of this fact from the isConcurrent document

In OS X version 10.6 and later, operational queues ignore the value returned by this method and always run operations in a separate thread.

+6
source

I have added a shorter answer now, leaving this for now, as that was part of the discussion ...


Well, I was pleased with my use of the isConcurrent method to this day!

I read the sentence:

In OS X v10.6 and later, operational queues ignore the value returned by this method and always run operations in a separate thread.

As a warning related to QA1712, indicating that for parallel operations, the start method can now be called on another thread for the one that queued the operation, which is a change in 10.6 and iOS 4.

I did not read this as an indication that the isConcurrent method isConcurrent completely ignored in the queue and has no purpose whatsoever, just that it no longer affects the thread that start is called on. Perhaps I misunderstood this.

I also misunderstood the original question as a more general question about concurrent operations and the isConcurrent flag, and the accepted answer is how to effectively say

isConcurrent flag can be ignored with 10.6 and iOS 4

I am not sure if this is correct.

If I now understand the original question, I rephrase:

Given a properly constructed parallel NSOperation , does the isConcurrent flag actually actually change the operation?

It's hard for me to say all the possible settings, but we can say that:

  • It is not out of date. It is normal for Apple to abandon methods that are no longer useful.

  • The documentation consistently states that the method is required to override.

isConcurrent may have isConcurrent effectively deprecated, but since it can use only one BOOL flag, you should not try to condemn it in documents. Or perhaps it is not doing anything right now, but Apple has saved it for possible future use and expects you to redefine it as described.

I created a quick test project with NSOperation , which overloaded only isConcurrent and main , isConcurrent not called at any stage. However, it was a very simple test. I suppose you might have tested it too? I suggested, perhaps, if NSOperationQueue did not call it NSOperation's default implementation of start .

So where does this leave us? Obviously, it is not difficult to implement and return YES to satisfy documented requirements. However, from my point of view, I think this is too big a step to go from the caution regarding 10.6 and iOS 4.0 to say that it can now be safely ignored.


My original answer ...

isConcurrent not an obsolete method and is not ignored by NSOperationQueue . The documentation provided in the other answers is a bit unclear and easy to understand.

isConcurrent = YES means the operation provides its own concurrency funds. Or else, the operation "isAlreadyConcurrent" does not need an NSOperationQueue to provide and manage concurrency. Since NSOperationQueue no longer provides concurrency, you need to specify it when the operation isFinished or if it isCancelled (etc.), Therefore, you must override these methods.

A common example is NSOperation , which manages NSURLConnection . NSURLConnection has its own mechanism for working in the background, so it does not need to be done simultaneously by NSOperationQueue .

The obvious question is this: "Why put an already parallel operation in NSOperationQueue ?" Thus, an operation can benefit from other NSOperationQueue dependency NSOperationQueue , etc.

The misleading part of the documentation refers only to the thread to which the start NSOperation method is NSOperation . This caused the problem described in QA1712.

+2
source

To paraphrase the original question:

For a correctly constructed parallel NSOperation does the value returned by isConcurrent really change the operation?

Although we could try to understand how the isConcurrent flag isConcurrent used by NSOperation , NSOperationQueue or other parts of the operating system, it would be a mistake to rely on any information that we found.

The purpose of the flag is described only as:

Returns YES if the operation is performed asynchronously with respect to the current thread, or NO if the operation is performed synchronously on any thread.

As long as you answer correctly, it should not be important who calls the method or how it affects any logic inside the Apple frameworks.


Additional notes:

The documentation makes reference to a change in the way NSOperationQueue used this value before and after OSX 10.6 and iOS 4.0.

In OS X v10.6, operational queues ignore the value returned by isConcurrent and always invoke the method to start your operation from a separate thread. However, in OS X v10.5, operation queues create a thread only if isConcurrent returns NO ....

This change caused the problem described in QA1712 .

This comment is usually interpreted as implying that the isConcurrent flag isConcurrent no longer used after 10.6 and iOS 4 and may be ignored. I disagree with this interpretation, as the method has not been officially deprecated and is still documented as a mandatory override for parallel operations.

Also, the fact that it was changed in the past is a reminder that it may change again in the future, so we must answer correctly, even if we suspect that it has no effect at the moment.

0
source

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


All Articles