1

In this case I have to consider the asynchronous callback as part of the task. I want only one task running at the same time.

Here is boilerplate:

func queueATask() {
    DispatchQueue.main.async {
         doSomeHeavyWorkPartA()
         //tunnelProvider is an instance of NETunnelProviderManager
         tunnelProvider.saveToPreferences {
              continueHeavyWorkPartB()
         }
    }
}

So I can make multiple calls of this function.But still before running a new task,the previous one must be finished (including partA and PartB).

queueATask()
queueATask()
queueATask()

I was considering DispatchGroup and DispatchSemaphore, but it needs to block the main queue which is not viable? All the tasks must be running in the main queue. If there is no partB callback block it would be much easier, I think this is the hard part for this situation.

Kindly guide me how to solve this issue.

Thanks

Li Fumin
  • 1,383
  • 2
  • 15
  • 31
  • Another way is an asynchronous Operation on a serial OperationQueue. – vadian Nov 06 '19 at 07:03
  • @vadian A little more details please? – Li Fumin Nov 06 '19 at 07:18
  • 1
    In [this question](https://stackoverflow.com/questions/43561169/trying-to-understand-asynchronous-operation-subclass) an asynchronous Operation is described. And [this is the documentation of OperationQueue](https://developer.apple.com/documentation/foundation/operationqueue) – vadian Nov 06 '19 at 07:24

0 Answers0