You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Timer initialiser init(timeInterval: TimeInterval, repeats: Bool, block: (Timer) -> Void) behaves wrongly on Linux.
if the user creates a non-scheduled timer and adds it to a runloop, the closure/block that is executed when the timer fires, also fires at creation time.
Demonstration Code
import Foundation
@main
public struct TimerRunloop {
public private(set) var text = "Hello, World!"
public static func main() {
// print(TimerRunloop().text)
let runLoop = RunLoop.current
let distantFuture = Date.distantFuture
/// Set this to false when we want to exit the app...
let shouldKeepRunning = true
// Set a scheduled timer going
print("\(Date()) Program starts.")
let timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { timer in
print("\(Date()) Timer fired.")
}
// Set a timer and add it tp the runloop manually
let timer2 = Timer(timeInterval: 5.0, repeats: true) { timer in
print("\(Date()) Timer2 fired")
}
// Make sure that it is scheduled onto the main thread.
DispatchQueue.main.async {
RunLoop.current.add(timer2, forMode: .default)
}
// Run forever
while shouldKeepRunning == true &&
runLoop.run(mode:.default, before: distantFuture) {}
}
}
Hello,
https://forums.swift.org/t/timer-behaviour-different-on-linux-and-macos-timer-block-executed-upon-timer-creation-too-early/63509
The
Timerinitialiserinit(timeInterval: TimeInterval, repeats: Bool, block: (Timer) -> Void)behaves wrongly on Linux.if the user creates a non-scheduled timer and adds it to a runloop, the closure/block that is executed when the timer fires, also fires at creation time.
Demonstration Code
Results:
macOS
Linux
The text was updated successfully, but these errors were encountered: