🗂 DispatchSourceTimer
protocol DispatchSourceTimer
[영문]
A dispatch source that submits the event handler block based on a timer. You do not adopt this protocol in your objects. Instead, use the makeTimerSource(flags:queue:) method to create an object that adopts this protocol.
[국문]
Dispatch Source는 타이머를 기반으로 이벤트 핸들러 코드 블럭을 제출합니다. 또한, DispatchSourceTimer 객체는 프로토콜을 채택할 필요가 없습니다. 대신에 makeTimerSource(flags:queue:) 함수를 사용하여 프로토콜을 채택하여 객체를 생성할 수 있습니다.
🗂 DispatchSourceTimer Scheduling the Timer Trigger Conditions
🌍 func schedule(deadline: DispatchTime, repeating: DispatchTimeInterval, leeway: DispatchTimeInterval)
- Schedules a timer with the specified deadline, repeat interval, and leeway values.
- 구체적인 마감시간, 반복 주기 그리고 여유 값을 통하여 타이머를 예약합니다.
🌍 func schedule(deadline: DispatchTime, repeating: Double, leeway: DispatchTimeInterval)
- Schedules a timer with the specified deadline, repeat interval, and leeway values.
- 구체적인 마감시간, 반복 주기 그리고 여유 값을 통하여 타이머를 예약합니다.
- Schedules a timer with the specified time, repeat interval, and leeway values.
- 구체적인 시간, 반복 주기 그리고 여유 값을 통하여 타이머를 예약합니다.
🌍 func schedule(wallDeadline: DispatchWallTime, repeating: Double, leeway: DispatchTimeInterval)
- Schedules a timer with the specified time, repeat interval, and leeway values.
- 구체적인 마감시간, 반복 주기 그리고 여유 값을 통하여 타이머를 예약합니다.
🗂 DispatchSourceTimer Example Source Code
📔 DispatchSourceTimer Example Swift Source Code 001
var timer: DispatchSourceTimer?
private func startTimer() {
let queue = DispatchQueue(label: "com.firm.app.timer", attributes: .concurrent)
timer?.cancel() // cancel previous timer if any
timer = DispatchSource.makeTimerSource(queue: queue)
timer?.schedule(deadline: .now(), repeating: .seconds(5), leeway: .milliseconds(100))
// or, in Swift 3:
//
// timer?.scheduleRepeating(deadline: .now(), interval: .seconds(5), leeway: .seconds(1))
timer?.setEventHandler { [weak self] in // `[weak self]` only needed if you reference `self` in this closure and you want to prevent strong reference cycle
print(Date())
}
timer?.resume()
}
private func stopTimer() {
timer?.cancel()
timer = nil
}
📔 DispatchSourceTimer Example Swift Source Code 002
var timer: DispatchSourceTimer? // note, unlike `Timer`, we have to maintain strong reference to GCD timer sources
func createTimer() {
timer = DispatchSource.makeTimerSource(queue: .main)
timer?.schedule(deadline: .now(), repeating: 1.0)
timer?.setEventHandler { [weak self] in // assuming you're referencing `self` in here, use `weak` to avoid strong reference cycles
// do something
}
// note, timer is not yet started; you have to call `timer?.resume()`
}
func startTimer() {
timer?.resume()
}
func pauseTiemr() {
timer?.suspend()
}
func stopTimer() {
timer?.cancel()
timer = nil
}
🚀 REFERENCE
DispatchSourceTimer - Dispatch | Apple Developer Documentation
Schedules a repeating timer with the specified deadline, repeat interval, and leeway values.
developer.apple.com
DispatchSourceTimer and Swift 3.0
I can't figure out how to make dispatch timer work repeatedly in Swift 3.0. My code: let queue = DispatchQueue(label: "com.firm.app.timer", attributes: DispatchQueue.Attr...
stackoverflow.com
A Background Repeating Timer in Swift
Background timers are a very usefool tool in one’s developer toolbox. Most of the time when we want to schedule a repeating unit of work we…
medium.com
Stop and restart a timer
I want to stop this timer and then restart it from where I stopped it. secondsTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(addSeconds), userInfo: nil, repeats:...
stackoverflow.com
댓글