0

I have a method that has this block inside:

func a(_ foo: () -> ()) { ... }
func b(_ foo: () -> ()) { ... }

func abc() {
    a {
        // some processing
        b {
            // some asynchronous work
        }
    }
}

When a button is tapped:

  1. I call method abc()
  2. It connects to the internet
  3. The point is that it takes time to do so

I am looking for a way to cancel the previous block, and run the current block if tapped twice.

0-1
  • 702
  • 1
  • 10
  • 30
Pawriwes
  • 283
  • 1
  • 6
  • 21
  • 1
    You're asking about cancellable closures. Check out this if you're targeting Swift 3 http://stackoverflow.com/a/39684520/1495682 – Kubba Oct 01 '16 at 19:53

1 Answers1

0

There is no direct option to cancel you're block. And block will be executed when it is called only one way to set condition in the body of the block and do not execute part that is inside.

In you're case try to use Operation and OperationQueue this approach will give you flexible solution to mange operation execution and gives opportunity to cancel operations.

Small example:

   let operationQueue = OperationQueue.main
   let operation = Operation()

   operationQueue.addOperation(operation)

   //Cancel operation that is executing
   operation.cancel()
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100