Skip to content

Instantly share code, notes, and snippets.

@amitshekhariitbhu
Last active January 26, 2017 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amitshekhariitbhu/a500f38abdd9318b1d4141435b0d4c30 to your computer and use it in GitHub Desktop.
Save amitshekhariitbhu/a500f38abdd9318b1d4141435b0d4c30 to your computer and use it in GitHub Desktop.
public class CompositeDisposableExampleActivity extends AppCompatActivity {
private final CompositeDisposable disposables = new CompositeDisposable();
@Override
protected void onCreate(Bundle savedInstanceState) {
doSomeWork();
}
@Override
protected void onDestroy() {
super.onDestroy();
// clearing it so that it does not send event after activity has been destroyed
disposables.();
}
void doSomeWork() {
disposables.add(sampleObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String value) {
}
}));
}
static Observable<String> sampleObservable() {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
// Do some long running operation
SystemClock.sleep(2000);
return Observable.just("one", "two", "three", "four", "five");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment