FXCollections fails if one does to keep the resulting collection referenced in memory
package application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
public class TestUnmodified {
public TestUnmodified() {
ObservableList<String> s = FXCollections.observableArrayList();
FXCollections.unmodifiableObservableList(s).addListener(new ListChangeListener<String>() {
@Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends String> c) {
TestUnmodified.this.call(c);
}
});
s.setAll("A1");
s.setAll("A2");
System.gc();
s.setAll("A3");
}
private void call(javafx.collections.ListChangeListener.Change<? extends String> c) {
System.err.println("MODIFIED " + c);
}
public static void main(String[] args) {
new TestUnmodified();
}
}
The only current fix and also reason e.g. Parent#getChildrenUnmodifiable is not affected is to keep the wrapped list in an instance variable
package application;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
public class TestUnmodified {
public TestUnmodified() {
ObservableList<String> s = FXCollections.observableArrayList();
FXCollections.unmodifiableObservableList(s).addListener(new ListChangeListener<String>() {
@Override
public void onChanged(
javafx.collections.ListChangeListener.Change<? extends String> c) {
TestUnmodified.this.call(c);
}
});
s.setAll("A1");
s.setAll("A2");
System.gc();
s.setAll("A3");
}
private void call(javafx.collections.ListChangeListener.Change<? extends String> c) {
System.err.println("MODIFIED " + c);
}
public static void main(String[] args) {
new TestUnmodified();
}
}
The only current fix and also reason e.g. Parent#getChildrenUnmodifiable is not affected is to keep the wrapped list in an instance variable