public final class WeakNotificationObserver extends Object implements NotificationObserver
NotificationObserver that only
holds a weak reference to the observer.
When using normal NotificationObserver in combination with the global NotificationObserver
the notification center will hold a reference to the observer as long as it isn't unregistered.
The observer itself will most likely hold a reference to the parent class which can prevent
the parent class from being garbage collected.
In many use cases this is not a problem but in some situations this can cause memory leaks.
For such situations this class can be used as a Wrapper around the normal notification observer. The usage should look like this:
public class MyClass {
private NotificationObserver observer;
...
public void someMethod() {
observer = (key, payload) -> {
// do something when observer is called.
};
notificationCenter.subscribe("some_topic", new WeakNotificationObserver(observer));
}
}
The example shows the following steps:
NotificationObserverWeakNotificationObserver
and pass the normal observer as constructor argumentUsing the pattern mentioned above will:
| Constructor and Description |
|---|
WeakNotificationObserver(NotificationObserver notificationObserver) |
| Modifier and Type | Method and Description |
|---|---|
void |
receivedNotification(String key,
Object... payload)
Handle the Notification which is passed by the NotificationCenter.
|
public WeakNotificationObserver(NotificationObserver notificationObserver)
public void receivedNotification(String key, Object... payload)
NotificationObserverreceivedNotification in interface NotificationObserverkey - notification namepayload - which are passedCopyright © 2019 Saxonia Systems AG. All rights reserved.