A system where your code can talk to itself
Issue: 12.5 (September/October 2014)
Author: Sam Rowlands
Author Bio: Sam is the codemancer behind Ohanaware Software.
Article Description: No description available.
Article Length (in bytes): 16,665
Starting Page Number: 16
Article Number: 12504
12504.zip Updated: 2014-09-05 0:20:29Related Link(s): None
Excerpt of article text...
Today I'm going to talk about in-app notifications. I don't mean user interface dialogs that pop-up to say something has been done, or the ones that appear when you've received a message. I'm going to explain an internal communication process that's used with Objective-C applications, and how this technique can be used to improve Xojo-made apps. It works wonderfully with threads!
In the diagram shown in Figure 1, you can see that I have three objects on a window: a thread, progress bar, and a timer. Now in order for the thread to update the progress bar, there has to be a timer which regularly polls the thread to find out its progress, and then it updates the progress bar. If the thread tries to directly update the progress bar, that will raise an exception.
How Do Notifications Help?
In Figure 2, you can see that the timer is gone, and instead we have a
NotificationManagerobject. The notification manager acts as a bridge between the thread and the progress bar. The progress bar registers itself with the
NotificationManageras a listener, then any time the Thread sends a message to the
NotificationManager, the message then gets forwarded to the Progress Bar.
The real power comes when you want to expand the process. For instance, you may have another window with another progress bar or even some code in the app that updates the dock icon. By also registering these objects for the messages, they'll automagically receive them when the thread sends the messages!
By removing the direct connection from the thread and the progress bar, we can easily get other objects to listen for the message, and if those objects no longer exist (say you close the second window) they no longer receive the messages—and you don't even have to write any extra code to handle any errors.
...End of Excerpt. Please purchase the magazine to read the full article.