Skip to content

Listener Pattern

Listener pattern is a very popular pattern in MVC design paradigm and most mobile frameworks. Pointr SDKs make heavy use of this pattern throughout the code. The most likely you will encounter this pattern is when working with Managers. Most managers (if not all) use this pattern to advertise events to their listeners.

For example, PoiManager advertises when new POI data is available through its onPoiManagerChangedPoisForSite callback method. Any party interested in the latest POI data should

  • Add itself as a listener to PoiManager - eg. PoiManager.addListener(self)
  • Implement callbacks of interest1 in your class

For all available callbacks, examine the relevant Listener class - eg. PoiManager’s callbacks are defined in PoiManager.Listener

Important note To avoid any race condition, always add listener before checking state/content. See below:

✅ Correct implementation

Pointr.shared.poiManager.addListener(...)
var pois = Pointr.shared.poiManager.pois(...)
In this implementation, you always ensure you have the latest data

❌ Incorrect implementation

(1) var pois = Pointr.shared.poiManager.pois(...)
(2) Pointr.shared.poiManager.addListener(...)
In this implementation, there is a risk that you may miss the callback, in case the update happens just after (1) and before (2) due to multi-threading.


1: Javascript and Swift allows you to implement as many callbacks as you are interested in whilst Java and Kotlin force you to implement all callbacks.


Last update: November 22, 2022
Back to top