I'm very new to the macOS/AppKit world, so please be patient if I don't adequately understand many macOS/AppKit concepts. Over the centuries (since the Ford administration), I've coded in many different environments, including several stints in operating system development, so programming isn't new to me.
I'm writing my first Swift app for my Mac. As a single-window app, I figured out what needed to be programed and it worked. Then, I decided to stretch it to be a multi-window app which worked save for a few new wrinkles.
One wrinkle is that I pass notifications from the app delegate to the view controllers. I've added observers in the view controller and send notifications. The problem is that every view controller receives the notification and attempts to process it, which may cause the program to crash, depending on it's state.
What I did to prevent this was to add a dictionary element (along with the other elements) containing the address of the view controller structure ("vcID" - view controller ID), which the receiving view controller examines to determine if the address is the same as the address of its own view controller. If it's not, the it just returns. If it is, it continues processing.
Simple, but obviously it's not efficient to wake up a bunch of threads only to have all but one just go back to sleep.
So, my question is: how do I notify a specific view controller in a Swift-flavored way to wake up and process the request?
Similarly, I want to know which view controller has just been given the focus as the mouse clicks in each window of the app. I've added an observer to the view controller to watch for the didBecomeKeyNotification, assuming that just one view controller would receive the notification. But again, as above, all view controllers wake up, not just the one that became key.
I suspect that both situations are caused by the same problem.
I'm very new to the macOS/AppKit world, so please be patient if I don't adequately understand many macOS/AppKit concepts. Over the centuries (since the Ford administration), I've coded in many different environments, including several stints in operating system development, so programming isn't new to me.
I'm writing my first Swift app for my Mac. As a single-window app, I figured out what needed to be programed and it worked. Then, I decided to stretch it to be a multi-window app which worked save for a few new wrinkles.
One wrinkle is that I pass notifications from the app delegate to the view controllers. I've added observers in the view controller and send notifications. The problem is that every view controller receives the notification and attempts to process it, which may cause the program to crash, depending on it's state.
What I did to prevent this was to add a dictionary element (along with the other elements) containing the address of the view controller structure ("vcID" - view controller ID), which the receiving view controller examines to determine if the address is the same as the address of its own view controller. If it's not, the it just returns. If it is, it continues processing.
Simple, but obviously it's not efficient to wake up a bunch of threads only to have all but one just go back to sleep.
So, my question is: how do I notify a specific view controller in a Swift-flavored way to wake up and process the request?
Similarly, I want to know which view controller has just been given the focus as the mouse clicks in each window of the app. I've added an observer to the view controller to watch for the didBecomeKeyNotification, assuming that just one view controller would receive the notification. But again, as above, all view controllers wake up, not just the one that became key.
I suspect that both situations are caused by the same problem.
Share Improve this question edited Mar 31 at 16:50 HangarRash 15.1k5 gold badges20 silver badges55 bronze badges asked Mar 31 at 8:24 user29306905user29306905 6 | Show 1 more comment1 Answer
Reset to default 0Credit for the complete explanation/answer to this question was posted back in 2012 by @Thilo in the context of a different question:
That "object" parameter to "addObserver" is an optional filter. Upon posting a notification you can set an object to the sender of the notification, and will then only be notified of that sender's events. If set to "nil" you will get all notification of this type (regardless who sent them).
My learning continues.
NotificationCenter.default.addObserver ( )
. Now the notification is only sent to the one app window that has the focus. I tripped over the solution in another stack overflow question. – user29306905 Commented Apr 1 at 1:48