在无限循环中等待自己,但用户每次都可以取消
Wait itself within endless loop, but user can cancel every time
在 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
循环本身需要运行一直等待用户
"stop",表示用户点击了停止按钮。
那是在浪费 CPU 权力吗?我怎样才能避免一直执行此循环?
您应该改用 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")
}
您可以将循环放入线程中并使其休眠,而不是使用外循环在线程中延迟执行。
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
}
}
您的更新包含在每 3 秒暂停一次的永恒 while 循环中。
停止是通过取消操作来管理的,而不是在循环中检查一些变量。
在 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
循环本身需要运行一直等待用户 "stop",表示用户点击了停止按钮。 那是在浪费 CPU 权力吗?我怎样才能避免一直执行此循环?
您应该改用 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")
}
您可以将循环放入线程中并使其休眠,而不是使用外循环在线程中延迟执行。
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
}
}
您的更新包含在每 3 秒暂停一次的永恒 while 循环中。
停止是通过取消操作来管理的,而不是在循环中检查一些变量。