0

I'm using a dispatch function to delay the action in my code:

                dispatch_after(dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(10 * Double(NSEC_PER_SEC))
                ), dispatch_get_main_queue(), { () -> Void in

                    //het delay thi thuc hien lenh o day

                    println("test") //or do something after 10 seconds here

            })

This function will print a test on my debug area after 10 second of delay. but for example, I want to remove that action before 10 second. How can do that? (i want to add a cancel button for the user if the user press a cancel button, It will remove the action that i setup to do in delay function before)

Martin R
  • 488,667
  • 78
  • 1,132
  • 1,248
Hieu Duc Pham
  • 1,064
  • 1
  • 10
  • 24
  • 1
    The easiest way is to create a flag that represents the cancel button, and once you've pressed the cancel, you set that flag to true, and you check that in the closure, and print/not print "test" in the closure according to it. Or if you navigate to a new viewcontroller after the cancel, you can use ˙weak self. – Dániel Nagy Mar 22 '15 at 08:42
  • Similar questions: http://stackoverflow.com/questions/28088014/cancel-dispatch-after-method, http://stackoverflow.com/questions/12475450/prevent-dispatch-after-background-task-from-being-executed, http://stackoverflow.com/questions/21795882/gcd-cancel-async-block, http://stackoverflow.com/questions/6090406/cancel-gcd-block-working-in-thread – Martin R Mar 22 '15 at 08:43
  • This can help me: http://stackoverflow.com/questions/28359768/cancel-a-timed-event-in-swift – Hieu Duc Pham Mar 22 '15 at 09:16

0 Answers0