Here's my GameLoop
class (Swift 3)
import UIKit
class GameLoop : NSObject {
var doSomething: () -> ()!
var displayLink : CADisplayLink!
init(doSomething: @escaping () -> ()) {
self.doSomething = doSomething
super.init()
start()
}
// you could overwrite this too
func handleTimer() {
doSomething()
}
func start() {
displayLink = CADisplayLink(target: self, selector: #selector(handleTimer))
/*
* If set to zero, the
* display link will fire at the native cadence of the display hardware.
* The display link will make a best-effort attempt at issuing callbacks
* at the requested rate.
*/
displayLink.preferredFramesPerSecond = 0
displayLink.add(to: .main, forMode: .commonModes)
}
func stop() {
displayLink.invalidate()
displayLink.remove(from: .main, forMode: .commonModes)
displayLink = nil
}
}
If you're updating something on a background GCD queue and want to pull those changes to the Main queue (and runloop), you should use Dispatch Source for Data.