How can you create an interface that always shows the latest information, but does not require a refresh button? For Objective-C developers the answer is to set up code to observe changes.
The Power Manager SDK includes the DSSWPMObserver object. This Objective-C/Cocoa object is wonderful because it makes writing responsive energy saving tools easy.
The process of observing changes is called Key Value Observation (KVO). Understanding and using KVO is a core feature of developing in Objective-C/Cocoa on the Mac and on iOS.
Typically KVO is used within an application. For Power Manager, we have extended KVO to work with any Power Manager connection, including those connected over a network. The same code that observes a change in Power Manager events on your Mac will work for observing changes in events on a remote Mac located on the other side of the planet. The networking is dealt with for you.
Let's jump into how to use Power Manager SDK's Key Value Observation support. We will do this by extending the connecting recipe's Xcode project.
This example will require no new code. The observation binding will be done entirely using Xcode's user interface tools.
Binding to Power Manager
Follow the steps in the connecting recipe to create a Power Manager aware project.
Your Objective-C Xcode project has a Power Manager connection and observer created in the application delegate. We are going to bind a label in the user interface to this observer.
- Select the MainMenu.xib in Xcode.
- Click on the Window object within the xib.
- Add a new label to the window.
- Bind the label's value to the application delegate object:
- Bind To: Test App Delegate
- Model Key Path: self.observer.scheduler.enabled
- Build and run the project.
The running application will show a window with either a 1 or a 0 for the label. It may not immediately seem exciting, but try starting and stopping Power Manager.
Your label will change to 1 when Power Manager is running, and 0 when Power Manager is stopped. This happens automatically and instantly thanks to the observation you created.
Try changing the Model Key Path to one of the following:
Every variable in the Power Manager Application Programmer Interface (API) can be bound to and observed.
Thanks to the DSSWPMObserver object you can use Objective-C/Cocoa's KVO mechanism to build responsive interfaces with very little code.