Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop a dispatchQueue in swift

I have a DispatchQueue for a introViewController that shows a gif for 11 seconds and then display my login page... But also have a button that skip the intro and display the login. When I click it the time still running and when I am navigating in the app goes back to the login when the time ends.

this is my class

class GifClass: UIViewController {

@IBOutlet weak var gifImage: UIImageView!
@IBOutlet weak var skipButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    gifImage.loadGif(name: "promed")

    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(11)) {

        self.performSegue(withIdentifier: "introLogin", sender: self)
    }

}


@IBAction func skip(_ sender: Any) {


    performSegue(withIdentifier: "introLogin", sender: self)

}

}

how do I do to stop the time when I click the button?

like image 410
Kevin Arvizu Avatar asked Dec 29 '17 00:12

Kevin Arvizu


People also ask

How do I cancel DispatchQueue?

You don't stop the queue. Instead, when the task that you dispatched starts executing, it needs to check its context and do the right thing (often: nothing) if the context has changed.

How do I cancel a GCD task?

You can't pause / cancel when using a GCD queue. If you need that functionality (and in a lot of general cases even if you don't) you should be using the higher level API - NSOperationQueue .

How do I cancel a dispatch work item?

A dispatch work item has a cancel flag. If it is cancelled before running, the dispatch queue won't execute it and will skip it. If it is cancelled during its execution, the cancel property return True. In that case, we can abort the execution.

What is DispatchQueue in Swift?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.


3 Answers

For this, you can use a DispatchWorkItem. Here is the reference:

https://developer.apple.com/documentation/dispatch/dispatchworkitem

And here is an example of how you could use it in your code:

class GifClass: UIViewController {

    @IBOutlet weak var gifImage: UIImageView!
    @IBOutlet weak var skipButton: UIButton!

    private var workItem: DispatchWorkItem? // Create a private DispatchWorkItem property

    override func viewDidLoad() {
        super.viewDidLoad()

        gifImage.loadGif(name: "promed")

        workItem = DispatchWorkItem { // Set the work item with the block you want to execute
            self.performSegue(withIdentifier: "introLogin", sender: self)
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(11), execute: workItem!)
    }

    @IBAction func skip(_ sender: Any) {
        workItem?.cancel() // Cancel the work item in the queue so it doesn't run
        performSegue(withIdentifier: "introLogin", sender: self)    
    } 
}
like image 129
Daniel Hall Avatar answered Sep 20 '22 21:09

Daniel Hall


I'm not sure if there are best practices here, but I would consider doing what you are doing with a Timer rather than the DispatchQueue.

class GifClass: UIViewController {

    @IBOutlet weak var gifImage: UIImageView!
    @IBOutlet weak var skipButton: UIButton!

    var timer = Timer()

    override func viewDidLoad() {
        super.viewDidLoad()

        gifImage.loadGif(name: "promed")

        timer = Timer.scheduledTimer(timeInterval: 11, target: self, selector: #selector(timerAction), userInfo: nil, repeats: false)
    }

    @objc func timerAction() {
        performSegue(withIdentifier: "introLogin", sender: self)
    }

    @IBAction func skip(_ sender: Any) {
        timer.invalidate()
        performSegue(withIdentifier: "introLogin", sender: self)
    }
}
like image 23
caesss Avatar answered Sep 19 '22 21:09

caesss


You don't stop the queue. Instead, when the task that you dispatched starts executing, it needs to check its context and do the right thing (often: nothing) if the context has changed.

like image 28
gnasher729 Avatar answered Sep 19 '22 21:09

gnasher729