2016-12-02 25 views
1

在Swift 3中,我有一個可以通過用戶按下按鈕來取消的循環。在循環內進行一些檢查。檢查後,任務可以睡一會兒。但隨着在無盡的循環中等待自己,但用戶可以每次取消

let delayQueue = DispatchQueue(label: "com.myApp.queue3", qos: .utility) 
let additionalTime: DispatchTimeInterval = .seconds(3) 

repeat { 
    delayQueue.asyncAfter(deadline: .now() + additionalTime) { self.update() } 
} while !self.stop 

本身運行所需要的所有等待用戶 時間循環調用任務時,「停止」,表示,用戶點擊停止按鈕。 是CPU浪費了嗎?我怎樣才能避免這個循環一直被完成?

回答

2

您應該使用Timer來代替。

var timer: Timer? 
let timeInterval: TimeInterval = 3 

func didPressCancelButton() { 
    timer?.invalidate() 
} 

func beginUpdates() { 
    timer = Timer.scheduledTimer(
     timeInterval: timeInterval, 
     target: self, 
     selector: #selector(self.update), 
     userInfo: nil, 
     repeats: true 
    ); 
} 

func update() { 
    print("Updated") 
} 
+0

謝謝,看起來像我喜歡它那麼容易。 –

+0

歡迎您! –

+0

一個問題:在上面的解決方案中,調用主DispatchQueue上的任務?因爲我沒有獲得在主隊列中運行的對話項的更新,所以我需要將它作爲不同的隊列啓動,例如,效用。 –

1

而不是延遲執行線程與外部循環,你可以把你的循環線程,而不是讓它睡覺。

import Foundation 

class YourUpdatingClass { 

    private let updateQueue: OperationQueue 
    init() { 
     updateQueue = OperationQueue() 
     updateQueue.name = "com.myApp.queue3" 
     updateQueue.qualityOfService = .utility 
    } 

    private var updateOperation: BlockOperation? 

    @IBAction func startUpdating() { 
     guard updateOperation == nil else { 
      // In case if updating already started 
      return 
     } 

     updateOperation = BlockOperation { [weak self] in 
      while true { 
       Thread.sleep(forTimeInterval: 3) 
       self?.update() 
      } 
     } 

     updateQueue.addOperation(updateOperation!) // we just created updateOperation, so we can use `!`, but use it with caution 
    } 

    @IBAction func stopUpdating() { 
     updateOperation?.cancel() 
     updateOperation = nil 
    } 

    private func update() { 
     print("update") // Whatever your update does 
    } 

} 

更新包含在永恆while循環中,每3秒鐘休息一次。

停止通過取消操作來管理,而不是在循環中檢查某些var。

+1

大。如果它變得更復雜,我會使用這個解決方案。這是更一般的解決方案。非常感謝! –