Skip to content

Commit ee1f028

Browse files
author
Kaushik Gopal
committed
fix: observable leak.
issue kaushikgopal#65 explains how to reproduce this and @marukami explains the reason for this happening beautifully. given that we want to allow multiple clicks of the button, i think the right away to address this is just by using `CompositeSubscription`s. In general, for android this also seems like the way to go. kaushikgopal#67 is also a good potential fix, but i prefer going the CS route. Seems more "semantic".
1 parent fb5cb28 commit ee1f028

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

app/src/main/java/com/morihacky/android/rxjava/fragments/ConcurrencyWithSchedulersDemoFragment.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@
1111
import android.widget.ArrayAdapter;
1212
import android.widget.ListView;
1313
import android.widget.ProgressBar;
14-
15-
import com.morihacky.android.rxjava.R;
16-
import com.morihacky.android.rxjava.RxUtils;
17-
18-
import java.util.ArrayList;
19-
import java.util.List;
20-
2114
import butterknife.Bind;
2215
import butterknife.ButterKnife;
2316
import butterknife.OnClick;
17+
import com.morihacky.android.rxjava.R;
18+
import java.util.ArrayList;
19+
import java.util.List;
2420
import rx.Observable;
2521
import rx.Observer;
2622
import rx.Subscription;
2723
import rx.android.schedulers.AndroidSchedulers;
2824
import rx.schedulers.Schedulers;
25+
import rx.subscriptions.CompositeSubscription;
2926
import timber.log.Timber;
3027

3128
public class ConcurrencyWithSchedulersDemoFragment
@@ -36,13 +33,13 @@ public class ConcurrencyWithSchedulersDemoFragment
3633

3734
private LogAdapter _adapter;
3835
private List<String> _logs;
39-
private Subscription _subscription;
36+
private CompositeSubscription _subscriptions = new CompositeSubscription();
4037

4138
@Override
4239
public void onDestroy() {
4340
super.onDestroy();
4441
ButterKnife.unbind(this);
45-
RxUtils.unsubscribeIfNotNull(_subscription);
42+
_subscriptions.clear();
4643
}
4744

4845
@Override
@@ -66,10 +63,12 @@ public void startLongOperation() {
6663
_progress.setVisibility(View.VISIBLE);
6764
_log("Button Clicked");
6865

69-
_subscription = _getObservable()//
66+
Subscription s = _getObservable()//
7067
.subscribeOn(Schedulers.io())
7168
.observeOn(AndroidSchedulers.mainThread())
72-
.subscribe(_getObserver()); // Observer
69+
.subscribe(_getObserver()); // Observer
70+
71+
_subscriptions.add(s);
7372
}
7473

7574
private Observable<Boolean> _getObservable() {

0 commit comments

Comments
 (0)