I’m trying to create a NSOperationQueue and add a NSOperation to it. Later I wan’t to check if the queue is running and maybe abort it. All of that is supposed to be called from within different functions. What’s the best approach to do this? I would be glad for a code example. Thanks!
Share
I would create an operation queue that’s managed by a singleton.
First, create your singleton class. It will provide access to the
NSOperationQueue. Let’s call the singletonMyGlobalQueueManager.It will have an
ivarcalledmyGlobalQueue:In the
.mfile ofMyGlobalQueueManager, create a fairly standardinitmethod that will set up the operation queue:Now, the method that provides itself as a singleton. Again, this is pretty standard stuff:
Let’s access that queue from wherever you want to use it:
You can then add operations to that queue as you fancy.
How to know if anythings queued?
How to abort? Cancel everything as follows:
Cancelling of course depends on the operations. If you’re writing custom
NSOperationclasses, you’ll need to handle that yourself.I find
NSOperationandNSOperationQueueto be fairly easy to use and quite straightforward.A great document to read for all this is the Concurrency Programming Guide. Specifically, have a look at Operation Queues