Sankar Narayanan
Sankar Narayanan

Reputation: 119

Advanced NSOperation - Add dependency at runtime

Is there a way to create an NSoperation at run time and add it as a dependency to an operation which is in progress and already in execute state?

If not, Is there any other alternative way to achieve this?

For example (use case where I need this):

Based on the response from server, I want to initiate a new NSOperation, whereas the web service itself is driven by a connection operation (which is already in progress ).

Now I want the connection operation to finish only after this response driven NSOperation finishes.

Reference from where I took the code:

https://developer.apple.com/sample-code/wwdc/2015/

Sample Code : Advanced NSOperations

Note :

  1. One option is that, I can alway add this NSOperation as a dependency and call finish if that response is not present in response. But still, my point is that I do not want to invoke that operation at all if I don't get that response.

  2. Another option is having this Connection operation to wait till that operation completes and then call finish() of this response driven NSOperation (This is indirect way of achieving the solution).

  3. The reason why I am trying to execute it in run time is that I want that module including that NSOperation to be pluggable from my project. When ever I don't want the module.. I shall remove the files and it should not affect the system.

Just wanted to know if some one came across the same scenario.

Upvotes: 2

Views: 903

Answers (1)

Gulypy
Gulypy

Reputation: 71

You can't attach dependencies to running NSOperations, and even if you could, it would not do much. The dependencies prevent their NSOperation from starting until all of them are finished, after the operation is already started there's no point left to them.

I'm sorry but I don't understand your use case. Could you clarify what your "connection operation" is ? Also, what would you expect to happen when attaching a new dependency to it ? Should it stop running ?

Upvotes: 0

Related Questions