Permalink
Browse files

fix: use a connectable observable instead of share

  • Loading branch information...
Kaushik Gopal
Kaushik Gopal committed Jul 13, 2015
1 parent 9aaa14f commit 1812c18064a1a508b3d704be21137b1e3ab868f0
@@ -14,9 +14,9 @@
import com.morihacky.android.rxjava.wiring.LogAdapter; import com.morihacky.android.rxjava.wiring.LogAdapter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import rx.Observable;
import rx.Observer; import rx.Observer;
import rx.functions.Action0; import rx.functions.Action0;
import rx.observables.ConnectableObservable;
import rx.subscriptions.CompositeSubscription; import rx.subscriptions.CompositeSubscription;
import timber.log.Timber; import timber.log.Timber;


@@ -82,13 +82,13 @@ public void startRetryingWithExponentialBackoffStrategy() {
} }


@Override @Override
public void observeResults(Observable<Integer> ints) { public void observeResults(ConnectableObservable<Integer> intsObservable) {


_subscriptions.add(// _subscriptions.add(//
ints.doOnSubscribe(new Action0() { intsObservable.doOnSubscribe(new Action0() {
@Override @Override
public void call() { public void call() {
_log("Subscribing to ints"); _log("Subscribing to intsObservable");
} }
}).subscribe(new Observer<Integer>() { }).subscribe(new Observer<Integer>() {
@Override @Override
@@ -107,6 +107,8 @@ public void onNext(Integer integer) {
_log(String.format("Worker frag spits out - %d", integer)); _log(String.format("Worker frag spits out - %d", integer));
} }
})); }));

intsObservable.connect();
} }


// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
@@ -7,11 +7,12 @@
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import rx.Observable; import rx.Observable;
import rx.functions.Func1; import rx.functions.Func1;
import rx.observables.ConnectableObservable;


public class RotationPersistWorkerFragment public class RotationPersistWorkerFragment
extends Fragment { extends Fragment {


private Observable<Integer> _intsObservable; private ConnectableObservable<Integer> storedIntsObservable;
private IAmYourMaster _masterFrag; private IAmYourMaster _masterFrag;


/** /**
@@ -45,11 +46,11 @@ public void onCreate(Bundle savedInstanceState) {
// Retain this fragment across configuration changes. // Retain this fragment across configuration changes.
setRetainInstance(true); setRetainInstance(true);


if (_intsObservable != null) { if (storedIntsObservable != null) {
return; return;
} }


_intsObservable =// Observable<Integer> intsObservable =//
Observable.interval(1, TimeUnit.SECONDS)// Observable.interval(1, TimeUnit.SECONDS)//
.map(new Func1<Long, Integer>() { .map(new Func1<Long, Integer>() {
@Override @Override
@@ -62,12 +63,13 @@ public Integer call(Long aLong) {
// ----------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------
// Making our observable "HOT" for the purpose of the demo. // Making our observable "HOT" for the purpose of the demo.


_intsObservable = _intsObservable.share(); //_intsObservable = _intsObservable.share();
storedIntsObservable = intsObservable.replay();


// Do not do this in production! // Do not do this in production!
// `.share` is "warm" not "hot" // `.share` is "warm" not "hot"
// the below forceful subscription fakes the heat // the below forceful subscription fakes the heat
_intsObservable.subscribe(); //_intsObservable.subscribe();
} }


/** /**
@@ -76,7 +78,7 @@ public Integer call(Long aLong) {
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
_masterFrag.observeResults(_intsObservable); _masterFrag.observeResults(storedIntsObservable);
} }


/** /**
@@ -90,6 +92,6 @@ public void onDetach() {
} }


public interface IAmYourMaster { public interface IAmYourMaster {
void observeResults(Observable<Integer> ints); void observeResults(ConnectableObservable<Integer> intsObservable);
} }
} }

0 comments on commit 1812c18

Please sign in to comment.