• invisible

NSNotification Troubles

July 18th, 2009 by eddie

We’re big fans of loose coupling and so make use of NSNotifications.

In Shotgun Free / Pro, for example, the achievements class listens for a notification to know when the shotgun has cocked, notes the time, listens for a notification to know when the shotgun has fired, notes the time, and then calculates the fire time as the difference between those two times.

This method was really simple to implement because the achievements class can exist essentially on its own, just listening and reacting without talking to any other classes. Sounds good, right?

Wrong. Unfortunately there are no guarantees on when those notifications are delivered. Everything works fine 99.9% of the time, but then once in a thousand fires the cocking notification won’t be delivered until just before the fire notification, giving you an absurdly fast fire time that sits around on your high score board forever. Oops.

Leave a Reply