narendrakothamire / android-binding

Automatically exported from code.google.com/p/android-binding
0 stars 0 forks source link

subscribe to observable is unstable #28

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create an observable property in a view model : public final 
BooleanObservable myProperty = new BooleanObservable();
2. Subscribe to the observable like this :      this.myProperty.subscribe(new 
Observer(){
            public void onPropertyChanged(IObservable<?> newVal, Collection<Object> arg1) {
                Log.d("", "INVOKED");
            }           
        });
3. Bind the myProperty to an input control, like an EditText, and change the 
value often.

What is the expected output? What do you see instead?

Expected : Always being called into the subscription.
What happens : After some times, we don't get called anymore in the 
subscription. The delay vary from tests to tests.

What version of the product are you using? On what operating system?

android-binding-v30-0.5.jar, targeting android 2.2

Please provide any additional information below.

Seems to be a weak reference issues since keeping a reference to the Observer 
fix the problem :

        this.myProperty.subscribe(myPropertyObs = new Observer(){
            public void onPropertyChanged(IObservable<?> newVal, Collection<Object> arg1) {
                Log.d("", "INVOKED");
            }           
        });

Original issue reported on code.google.com by instri...@hotmail.com on 18 Jul 2012 at 1:26

GoogleCodeExporter commented 8 years ago
Since the subscription is to weak reference (in a weak list), better not to 
provide anonymous class in this way. Rather, create a variable holding that 
observer instance would prevent this. 

Original comment by gueei....@gmail.com on 18 Jul 2012 at 4:08

GoogleCodeExporter commented 8 years ago
I don't think api "users" should know about the internal design being a weak 
list to use it well. In this case, for each observable we subscribed to, we 
need to bother to keep the reference alive so the subscription works. 

I think the subscription should stay alive as long as the observable is (or 
until we manually unsuscribe). Right now, it make pollutions for every 
subscription we do in our view model, without talking about the risk of 
forgetting to keep a reference and making the code unstable. For exemple, I 
have 10 observers of this kind at the top on only 1 view model (I know I could 
have a collection of observers but still...) :

@SuppressWarnings("unused")
private Observer _myDummyObserver;

Also, it seems a lot more natural to use the anonymous class (they are also 
used this way in test testInnerField_Notification_OA_OB_OC for exemple).

Original comment by instri...@hotmail.com on 19 Jul 2012 at 2:28