Im trying to detect events of focus change(when the active window changed) in swift, by adding observer like this:
func createObserver() {
var observer = NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.didActivateApplicationNotification,
object: NSWorkspace.shared,
queue: nil,
using: {_ in
print("got focus change event"))}
}
this is just an example how Im using this code in my project
this method(createObserver() runs from a background thread).
In my project, Im using dispatch_main() in the main function of the project(who is in c++,Xcode).
Im only getting the handler(the print), when Im switching the dispatch_main() into CFRunLoopRun() on the main thread, but i do not know if i can do it and what it can cause?
I want to know why i do not get the handler called if Im running RunLoop.current.run() on the thread which runs the createObserver function ? why i must run the run loop from the main thread? I have tried almost everything.
I also tried to add a source to this specific run loop, but i dont see a way to connect between the source and the specific notification.
Is there any other way to get the notifications, and not to delete the dispatch_main ?
I want to run the RunLoop from a background thread, for not to block my program.
Thank you !!