2

Refer this video from WWDC https://developer.apple.com/videos/play/wwdc2015/226/ The speaker shows that we can add dependency between two NSopeation instances of same type. Example an NSoperation that displays an alert. By achieving this we can make sure that we don't throw multiple alerts at same time and annoy the user. If one alert is already being displayed next one will wait.

I still can't figure out how to implement this dependency of NSOperations cross queue.In more simpler words can anyone show an example(implementation) of following two things.

1.Implementation of adding dependency of operation B from queue 2 on operation A from queue 1.

2.Implementation of adding dependency of multiple instances of same NSOperation type, even if they are in different queue. Example: if i add multiple instances of "AlertOperation" to different queue I want to make sure they still take place sequentially among themselves.

I would appreciate if the examples are in Objective C. Please ask for more clarification if needed.

Aditya Gaonkar
  • 660
  • 6
  • 13
  • Explain what your different queues are for. How and when are they created and the operations added? – Wain Mar 02 '16 at 07:47
  • Purpose of queues could be anything. The point is, in the WWDC video which i refereed above says, that i can have alert operation in different queues and make them dependent on each other. Now if alert operation from queue 1 is already displaying an alert, then i want alert operation from queue 2 to wait displaying until the first alert operation is over(i.e alert is dismissed). This will make sure that i dont pop another alert when user already has one alert on screen which he is looking at. – Aditya Gaonkar Mar 04 '16 at 11:57
  • and i assume that adding a dependency between them gave you some problem when you tried it? – Wain Mar 04 '16 at 12:13
  • Not exactly. Question is how to add dependency between nsoperation from different queues. Like i explained in main question above with example. – Aditya Gaonkar Mar 05 '16 at 17:32
  • When you added a dependency between them what happened? – Wain Mar 05 '16 at 18:23
  • Thats the question. I know how to add dependency between nsoperations in same queue bt dont know How to add dependancy between two nsoperations from different queues. – Aditya Gaonkar Mar 06 '16 at 04:16
  • Just the same. I haven't tried it but I imagine it works fine. – Wain Mar 06 '16 at 07:23
  • That is fine when you have reference to both operations. How would u do it when both operations are added at run time.Please refer the video and understand the purpose of adding dependency between two operations of same type but in different queue. That's exactly i want to know. – Aditya Gaonkar Mar 07 '16 at 06:30
  • You don't need to use operations to fix the problem with the new UIAlertController: http://stackoverflow.com/a/35211571/259521 – malhal Sep 23 '16 at 07:06

2 Answers2

8

I'm the engineer who presented that session.

The short answer is that in order to make your second operation dependent on the first operation, you have to maintain a reference to the first operation.

The sample code provided with the session uses a global table that keeps track of all the currently-executing operations. When a new operation comes in that specifies it should be mutually exclusive with other operations of the same kind, the code looks up in the table for the other operations of the same kind. The new operation is then made dependent on the last one in the list.

Since the table is a global table, it works regardless of which queue the operations are actually executing on. The only thing it requires is using the custom NSOperationQueue subclass ("OperationQueue") as the thing that's executing operations.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Kudos! I was in process of implementing something similar. But was not sure if that's the best approach. Thanks Dave. – Aditya Gaonkar Mar 09 '16 at 03:51
  • 1
    Can we wish for this year's a followup session which demonstrates nice technique to pass data from one NSOperation to another without using temporary file? – Mindaugas Apr 19 '16 at 10:59
  • @Mindaugas, if you have access to the forum I found a nice reply from a Apple dev here: [https://forums.developer.apple.com/thread/25761](https://forums.developer.apple.com/thread/25761) – GrizzlyBear Dec 14 '17 at 13:16
0

From the comments, the underlying question is:

how can I add a dependency to an existing operation when I don't have a reference to it

You should create multiple different queues, and specifically in this case a queue just for alert operations. Technically it can work with a single queue, but you need to do a bit more work.

With a specific queue you can simply iterate the operations currently on the queue and add a dependency to every one. If you don't have a specific queue then you'll need to do a class test (or use some other logic) to decide exactly which operations to add a dependency to.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • We are digressing here a bit. My question is still those two points which are in main question body above. – Aditya Gaonkar Mar 07 '16 at 09:30
  • this answers those questions, you need to either group the operations on a specific queue so you can find them or you need to search the operations on the queue(s) to find the correct ones – Wain Mar 07 '16 at 10:09