Permalink
Browse files

fix: use unsubscriptions.clear instead of unsubscribe

See Dan's point in this:
https://speakerdeck.com/dlew/common-rxjava-mistakes

unsubscribe terminates future subscriptions
  • Loading branch information...
Kaushik Gopal
Kaushik Gopal committed Apr 8, 2016
1 parent e526882 commit 1e7d4b2f867a97b32a0cde81cb488c3d17d4952f
Showing with 58 additions and 96 deletions.
  1. +0 −9 app/src/main/java/com/morihacky/android/rxjava/RxUtils.java
  2. +2 −2 app/src/main/java/com/morihacky/android/rxjava/fragments/BufferDemoFragment.java
  3. +1 −1 app/src/main/java/com/morihacky/android/rxjava/fragments/ConcurrencyWithSchedulersDemoFragment.java
  4. +3 −2 app/src/main/java/com/morihacky/android/rxjava/fragments/DebounceSearchEmitterFragment.java
  5. +1 −2 app/src/main/java/com/morihacky/android/rxjava/fragments/DoubleBindingTextViewFragment.java
  6. +1 −8 app/src/main/java/com/morihacky/android/rxjava/fragments/ExponentialBackoffFragment.java
  7. +1 −7 app/src/main/java/com/morihacky/android/rxjava/fragments/FormValidationCombineLatestFragment.java
  8. +11 −3 app/src/main/java/com/morihacky/android/rxjava/fragments/PollingFragment.java
  9. +0 −8 app/src/main/java/com/morihacky/android/rxjava/fragments/PseudoCacheConcatFragment.java
  10. +3 −11 app/src/main/java/com/morihacky/android/rxjava/fragments/PseudoCacheMergeFragment.java
  11. +15 −17 app/src/main/java/com/morihacky/android/rxjava/fragments/RetrofitFragment.java
  12. +1 −8 app/src/main/java/com/morihacky/android/rxjava/fragments/RotationPersist1Fragment.java
  13. +1 −8 app/src/main/java/com/morihacky/android/rxjava/fragments/RotationPersist2Fragment.java
  14. +2 −1 app/src/main/java/com/morihacky/android/rxjava/fragments/TimeoutDemoFragment.java
  15. +5 −2 app/src/main/java/com/morihacky/android/rxjava/fragments/TimingDemoFragment.java
  16. +1 −1 app/src/main/java/com/morihacky/android/rxjava/rxbus/RxBusDemo_Bottom1Fragment.java
  17. +1 −1 app/src/main/java/com/morihacky/android/rxjava/rxbus/RxBusDemo_Bottom2Fragment.java
  18. +9 −5 app/src/main/java/com/morihacky/android/rxjava/volley/VolleyDemoFragment.java
@@ -1,7 +1,6 @@
package com.morihacky.android.rxjava;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public class RxUtils {

@@ -10,12 +9,4 @@ public static void unsubscribeIfNotNull(Subscription subscription) {
subscription.unsubscribe();
}
}

public static CompositeSubscription getNewCompositeSubIfUnsubscribed(CompositeSubscription subscription) {
if (subscription == null || subscription.isUnsubscribed()) {
return new CompositeSubscription();
}

return subscription;
}
}
@@ -53,8 +53,8 @@
private Subscription _subscription;

@Override
public void onStart() {
super.onStart();
public void onResume() {
super.onResume();
_subscription = _getBufferedSubscription();
}

@@ -42,8 +42,8 @@
@Override
public void onDestroy() {
super.onDestroy();
RxUtils.unsubscribeIfNotNull(_subscription);
ButterKnife.unbind(this);
RxUtils.unsubscribeIfNotNull(_subscription);
}

@Override
@@ -15,7 +15,6 @@
import com.jakewharton.rxbinding.widget.RxTextView;
import com.jakewharton.rxbinding.widget.TextViewTextChangeEvent;
import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;

import java.util.ArrayList;
import java.util.List;
@@ -24,9 +23,11 @@
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import co.kaush.core.util.CoreNullnessUtils;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import timber.log.Timber;

import static java.lang.String.format;
@@ -45,7 +46,7 @@
@Override
public void onDestroy() {
super.onDestroy();
RxUtils.unsubscribeIfNotNull(_subscription);
_subscription.unsubscribe();
ButterKnife.unbind(this);
}

@@ -9,7 +9,6 @@
import android.widget.TextView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
@@ -70,7 +69,7 @@ public void onNumberChanged() {
@Override
public void onDestroyView() {
super.onDestroyView();
RxUtils.unsubscribeIfNotNull(_subscription);
_subscription.unsubscribe();
ButterKnife.unbind(this);
}
}
@@ -9,7 +9,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.wiring.LogAdapter;

import java.util.ArrayList;
@@ -38,12 +37,6 @@

private CompositeSubscription _subscriptions = new CompositeSubscription();

@Override
public void onResume() {
super.onResume();
_subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -63,7 +56,7 @@ public View onCreateView(LayoutInflater inflater,
public void onPause() {
super.onPause();

RxUtils.unsubscribeIfNotNull(_subscriptions);
_subscriptions.clear();
}

@Override
@@ -10,7 +10,6 @@

import com.jakewharton.rxbinding.widget.RxTextView;
import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
@@ -55,16 +54,11 @@ public View onCreateView(LayoutInflater inflater,
return layout;
}

@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscription);
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
_subscription.unsubscribe();
}

private void _combineLatestEvents() {
@@ -12,7 +12,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;

import java.util.ArrayList;
import java.util.List;
@@ -40,9 +39,18 @@

private LogAdapter _adapter;
private List<String> _logs;
private CompositeSubscription _subscriptions = new CompositeSubscription();

private CompositeSubscription _subscriptions;
private int _counter = 0;


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

_subscriptions = new CompositeSubscription();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -61,7 +69,7 @@ public View onCreateView(LayoutInflater inflater,
@Override
public void onDestroy() {
super.onDestroy();
RxUtils.unsubscribeIfNotNull(_subscriptions);
_subscriptions.unsubscribe();
ButterKnife.unbind(this);
}

@@ -9,7 +9,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.retrofit.Contributor;
import com.morihacky.android.rxjava.retrofit.GithubApi;
import com.morihacky.android.rxjava.retrofit.GithubService;
@@ -33,7 +32,6 @@

@Bind(R.id.log_list) ListView _resultList;

private Subscription _subscription = null;
private HashMap<String, Long> _contributionMap = null;
private ArrayAdapter<String> _adapter;

@@ -47,12 +45,6 @@ public View onCreateView(LayoutInflater inflater,
return layout;
}

@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscription);
}

@Override
public void onDestroyView() {
super.onDestroyView();
@@ -10,7 +10,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.retrofit.Contributor;
import com.morihacky.android.rxjava.retrofit.GithubApi;
import com.morihacky.android.rxjava.retrofit.GithubService;
@@ -24,7 +23,6 @@
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
@@ -34,27 +32,21 @@
extends BaseFragment {

@Bind(R.id.log_list) ListView _resultList;

private ArrayAdapter<String> _adapter;
private HashMap<String, Long> _contributionMap = null;
private HashMap<Contributor, Long> _resultAgeMap = new HashMap<>();
private Subscription _subscription = null;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_pseudo_cache_concat, container, false);
ButterKnife.bind(this, layout);
_initializeCache();
return layout;
}

@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscription);
}

@Override
public void onDestroyView() {
super.onDestroyView();
@@ -10,17 +10,19 @@
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.retrofit.Contributor;
import com.morihacky.android.rxjava.retrofit.GithubApi;
import com.morihacky.android.rxjava.retrofit.GithubService;
import com.morihacky.android.rxjava.retrofit.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
@@ -42,13 +44,15 @@

private ArrayAdapter<String> _adapter;
private GithubApi _githubService;
private CompositeSubscription _subscriptions = new CompositeSubscription();
private CompositeSubscription _subscriptions;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String githubToken = getResources().getString(R.string.github_oauth_token);
_githubService = GithubService.createGithubService(githubToken);

_subscriptions = new CompositeSubscription();
}

@Override
@@ -66,24 +70,18 @@ public View onCreateView(LayoutInflater inflater,
return layout;
}

@Override
public void onResume() {
super.onResume();
_subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions);
}

@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscriptions);
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}

@Override
public void onDestroy() {
super.onDestroy();
_subscriptions.unsubscribe();
}

@OnClick(R.id.btn_demo_retrofit_contributors)
public void onListContributorsClicked() {
_adapter.clear();
@@ -10,7 +10,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.wiring.LogAdapter;

import java.util.ArrayList;
@@ -92,12 +91,6 @@ public void onNext(Integer integer) {
// Boilerplate
// -----------------------------------------------------------------------------------

@Override
public void onResume() {
super.onResume();
_subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -116,7 +109,7 @@ public View onCreateView(LayoutInflater inflater,
@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscriptions);
_subscriptions.clear();
}

@Override
@@ -10,7 +10,6 @@
import android.widget.ListView;

import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.RxUtils;
import com.morihacky.android.rxjava.wiring.LogAdapter;

import java.util.ArrayList;
@@ -91,12 +90,6 @@ public void onNext(Integer integer) {
// Boilerplate
// -----------------------------------------------------------------------------------

@Override
public void onResume() {
super.onResume();
_subscriptions = RxUtils.getNewCompositeSubIfUnsubscribed(_subscriptions);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@@ -115,7 +108,7 @@ public View onCreateView(LayoutInflater inflater,
@Override
public void onPause() {
super.onPause();
RxUtils.unsubscribeIfNotNull(_subscriptions);
_subscriptions.clear();
}

private void _setupLogger() {
Oops, something went wrong.

0 comments on commit 1e7d4b2

Please sign in to comment.