This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Idle-ing when no one's interested
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
I have an object which polls data. Anytime it gets meaningful data, it triggers an event. The problem with this object is that it creates and maintains a process. And processes are one of those objects that don't just magically detect when you're not interested in what they're doing and GC. You have to turn them off. If you've got an object with 0...N interested observers, who decided when to stop the process?
In this case, it turns out that if I could determine when I have 1 or more observers and when that changes, I could start and stop the process accordingly. So I set to do such a thing.
If you've done your own efficiently observed object, then you know that doing so is a matter of three things:
Add an ivar to your object (often called eventHandlers).
implement myEventTable which returns your new ivar.
implement myEventTable: which sets your new ivar as the argument .
Armed with such a knowledge, I figured I could just override myEventTable: and update my data gathering process state there. Thinking that myEventTable: was used anytime a change was made to the observable's observed state. Turns out that's not quite what you want though.
There's another method called setActionList:forEvent:. This appears to be the work horse of event table management, including removal. By implementing:
i was able to automagically have the process run/stop based on whether anybody was listening. Seems to work pretty well so far, at least the following test seemed to pass: