Skip to content

Instantly share code, notes, and snippets.

@PhilippeBoisney
Last active December 16, 2017 15:05
Show Gist options
  • Save PhilippeBoisney/ed7f76e76fb62ab0fbfce4022d9779ef to your computer and use it in GitHub Desktop.
Save PhilippeBoisney/ed7f76e76fb62ab0fbfce4022d9779ef to your computer and use it in GitHub Desktop.
public class MainFragment extends Fragment {
...
// ------------------
// RX JAVA
// ------------------
// 3 - Create an observable that will run a long request...
private Observable<String> getObservable(){
return Observable.just("Long process is ended !")
.delay(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
// 4 - Create a observer that will subscribe on it
private DisposableObserver<String> getSubscriber(){
return new DisposableObserver<String>() {
@Override
public void onNext(String result) {
Log.e("TAG","On Next");
updateUIWhenStoppingOperation(result);
}
@Override
public void onError(Throwable e) {
Log.e("TAG","On Error");
}
@Override
public void onComplete() {
Log.e("TAG","On Complete !!");
}
};
}
...
// ------------------
// UPDATE UI
// ------------------
// 5 - We update a TextView when observable starts
private void updateUIWhenStartingOperation(){
this.textView.setText("Starting long process...");
}
// 6 - We update a TextView with result of observable
private void updateUIWhenStoppingOperation(String result){
this.textView.setText(result);
}
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment