-1

I want to enhance the code below: when i click the "submitData" button, the added code should cancel the completion handler.

func returnUserData(completion:(result:String)->Void){
  for index in 1...10000 {
     print("\(index) times 5 is \(index * 5)")
  }

  completion(result: "END");

}

func test(){
  self.returnUserData({(result)->() in
     print("OK")
  })
}

@IBAction func submintData(sender: AnyObject) {
    self.performSegueWithIdentifier("TestView", sender: self)
}

Can you tell me how to do this?

Prune
  • 76,765
  • 14
  • 60
  • 81
Vykintas
  • 401
  • 1
  • 8
  • 23
  • Is `returnUserData` really doing some loop like this, or is it doing something that might already support cancellation of asynchronous actions (e.g. network request, etc.)? – Rob Nov 07 '15 at 01:32

1 Answers1

1

You can use NSOperation subclass for this. Put your calculation inside the main method, but periodically check cancelled, and if so, break out of the calculation.

For example:

class TimeConsumingOperation : NSOperation {
    var completion: (String) -> ()

    init(completion: (String) -> ()) {
        self.completion = completion
        super.init()
    }

    override func main() {
        for index in 1...100_000 {
            print("\(index) times 5 is \(index * 5)")

            if cancelled { break }
        }

        if cancelled {
            completion("cancelled")
        } else {
            completion("finished successfully")
        }
    }
}

Then you can add the operation to an operation queue:

let queue = NSOperationQueue()

let operation = TimeConsumingOperation { (result) -> () in
    print(result)
}
queue.addOperation(operation)

And, you can cancel that whenever you want:

operation.cancel()

This is, admittedly, a fairly contrived example, but it shows how you can cancel your time consuming calculation.

Many asynchronous patterns have their built-in cancelation logic, eliminating the need for the overhead of an NSOperation subclass. If you are trying to cancel something that already supports cancelation logic (e.g. NSURLSession, CLGeocoder, etc.), you don't have to go through this work. But if you're really trying to cancel your own algorithm, the NSOperation subclass handles this quite gracefully.

Rob
  • 415,655
  • 72
  • 787
  • 1,044