Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Created July 28, 2015 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakTaccardi/c1119fe37a8a5f97eea6 to your computer and use it in GitHub Desktop.
Save ZakTaccardi/c1119fe37a8a5f97eea6 to your computer and use it in GitHub Desktop.
List<Integer> coolList = new ArrayList<Integer>();
coolList.add(1);
coolList.add(2);
coolList.add(3);
Observable.from(coolList).map(new Func1<Integer, Long>() {
@Override
public Long call(Integer integer) {
//runs on the IO background thread because we specify `Schedulers.io()`
return integer * 10000000000L;
}
})
//this schedulers tells all previous observables to run on the IO scheduler
.subscribeOn(Schedulers.io())
//this schedulers tells the subscriber to run on the main thread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Long>() {
//all these methods run on the main thread because of the `AndroidSchedulers.mainThread()`
@Override
public void onCompleted() {
System.out.println("This went perfectly! this is only called once, and cannot be called if onError is called");
}
@Override
public void onError(Throwable e) {
System.out.println("There was an error! this is only called once, and cannot be called if onCompleted is called");
}
@Override
public void onNext(Long l) {
System.out.println("Emitted number: " + l);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment