【问题标题】:how to stop a dispatchQueue in swift如何快速停止 dispatchQueue
【发布时间】:2018-06-09 12:16:14
【问题描述】:

我有一个用于 introViewController 的 DispatchQueue,它显示一个 gif 11 秒,然后显示我的登录页面......但也有一个按钮可以跳过介绍并显示登录。当我单击它时,时间仍在运行,当我在应用程序中导航时,时间结束时会返回登录。

这是我的课

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)

}

}

当我点击按钮时如何停止时间?

【问题讨论】:

    标签: swift dispatch-queue


    【解决方案1】:

    为此,您可以使用DispatchWorkItem。这是参考:

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

    下面是如何在代码中使用它的示例:

    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)    
        } 
    }
    

    【讨论】:

      【解决方案2】:

      我不确定这里是否有最佳实践,但我会考虑使用 Timer 而不是 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)
          }
      }
      

      【讨论】:

        【解决方案3】:

        您不会停止队列。相反,当您分派的任务开始执行时,它需要检查其上下文并在上下文发生变化时做正确的事情(通常:什么都不做)。

        【讨论】:

        • 所以,我需要使用定时器。
        • 实际上有一个 API 用于取消 DispatchQueue 中的块。请参阅下面的答案
        猜你喜欢
        • 2015-10-18
        • 2012-08-09
        • 1970-01-01
        • 2021-05-22
        • 2021-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多