I'm interested in getting notified when the currentMode property of the RunLoop class changes, more specifically, I'm interested in getting an event when the mode is entering .tracking
state.
I've tried two different approaches:
This one simply doesn't work, what could be wrong with it?:
import Foundation
public final class RunLoopTracker {
private let runLoop: RunLoop
private var observation: NSKeyValueObservation?
public init(runLoop: RunLoop) {
self.runLoop = runLoop
}
public func attach() {
observation = runLoop.observe(\.currentMode) { runLoop, change in
print(change)
}
}
}
This one works, but fires only once. I'd like to get the block executed each time the RunLoop
enters the specific mode:
import Foundation
public final class RunLoopTracker2 {
private let runLoop: RunLoop
private var observation: NSKeyValueObservation?
public init(runLoop: RunLoop) {
self.runLoop = runLoop
}
public func attach() {
runLoop.perform(inModes: [.tracking]) {
print("Entering the tracking mode, send notification")
}
}
}
What could be the solution to these two problems or a different approach to track RunLoop.currentMode
changes?