Last active
January 26, 2017 09:43
-
-
Save amitshekhariitbhu/a500f38abdd9318b1d4141435b0d4c30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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