Stop a DispatchQueue that is running on the main thread

后端 未结 2 761
既然无缘
既然无缘 2020-12-10 15:53

I have this block of code:

    DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) {
        self.isS         


        
2条回答
  •  余生分开走
    2020-12-10 16:38

    You can use DispatchWorkItems. They can be scheduled on DispatchQueues and cancelled before their execution.

    let work = DispatchWorkItem(block: {
        self.isShootingOnHold = false
        self.shoot()
        self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
    })
    DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay, execute: work)
    work.cancel()
    

提交回复
热议问题