The exact problem is most probably more general than this, but here's a specific scenario where it occurs. I leave it for you to pinpoint the issue behind it.
So I have cases where I need to remove my InvalidationListener upon first notification, as follows:
observable.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
doSomething();
observable.removeListener(this);
}
}
And this works perfectly. However, I noticed that in the case where I have added to my 'observable' two other listeners, say in this order:
observable.addListener(weakInvalidationListener1);
observable.addListener(weakInvalidationListener2);
observable.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
doSomething();
observable.removeListener(this);
}
}
In this case, upon calling 'observable.removeListener(this);' the 'invalidationSize' inside the property is being properly decremented from 3 to 2, but the listener is still appearing in the 'invalidationListeners' array.
I took a look at the removeListener implementation, and I think the issue is related to both the 'invalidationSize' and the index of the listener in question, because weirdly enough, the following issue doesn't occur in all the following scenarios:
- if I add only one weakInvalidationListener then my invalidationListener (the one to be removed)
- or if I add my invalidationListener first, then weakInvalidationListener1, then weakInvalidationListener2
- or if I add weakInvalidationListener1, then my invalidationListener, then weakInvalidationListener2
So I have cases where I need to remove my InvalidationListener upon first notification, as follows:
observable.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
doSomething();
observable.removeListener(this);
}
}
And this works perfectly. However, I noticed that in the case where I have added to my 'observable' two other listeners, say in this order:
observable.addListener(weakInvalidationListener1);
observable.addListener(weakInvalidationListener2);
observable.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
doSomething();
observable.removeListener(this);
}
}
In this case, upon calling 'observable.removeListener(this);' the 'invalidationSize' inside the property is being properly decremented from 3 to 2, but the listener is still appearing in the 'invalidationListeners' array.
I took a look at the removeListener implementation, and I think the issue is related to both the 'invalidationSize' and the index of the listener in question, because weirdly enough, the following issue doesn't occur in all the following scenarios:
- if I add only one weakInvalidationListener then my invalidationListener (the one to be removed)
- or if I add my invalidationListener first, then weakInvalidationListener1, then weakInvalidationListener2
- or if I add weakInvalidationListener1, then my invalidationListener, then weakInvalidationListener2