Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 1.02 KB

script.md

File metadata and controls

27 lines (19 loc) · 1.02 KB

RunLoop for Precise Timing in Swift

Utilize Swift's RunLoop for high-precision timing tasks. By adding a timer to the RunLoop with common mode, you ensure more accurate and consistent execution of timed operations, even amidst other ongoing events like UI updates. This approach provides a significant advantage over basic timers for scenarios demanding exact timing, like performing actions every second with precision. However, be cautious with its use to avoid potential performance impacts due to the extra load on the thread.

import Foundation

// Imagine this function does something amazing every second
func performPrecisionTask() {
    print("Task performed at \(Date())")
}

// Creating a timer
let timer = Timer(timeInterval: 1.0, repeats: true) { _ in
    performPrecisionTask()
}

// Adding the timer to the current RunLoop
RunLoop.current.add(timer, forMode: .common)

// Keeping the main thread alive
RunLoop.current.run()

Reference

YouTube 👀