Skip to content

Instantly share code, notes, and snippets.

@Nyame123
Created November 5, 2020 22:49
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 Nyame123/b8c70ecb346eb1ef94b3881aa97ee982 to your computer and use it in GitHub Desktop.
Save Nyame123/b8c70ecb346eb1ef94b3881aa97ee982 to your computer and use it in GitHub Desktop.
comparing defer with just usage
package com.bisapp.rxjavaexamples;
import java.util.concurrent.Callable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
public class Book {
String page = "First page";
public void setPage(String page) {
this.page = page;
}
public Observable<String> justObservable() {
return Observable.just(page);
}
public Observable<String> deferObservable() {
return Observable.defer(new Callable<ObservableSource<String>>() {
@Override
public ObservableSource<String> call() throws Exception {
return Observable.just(page);
}
});
}
}
//Calling the example
private void deferExample(){
Book newBook = new Book();
Observable<String> justObservable = newBook.justObservable();
Observable<String> deferObservable = newBook.deferObservable();
newBook.setPage("Second Page");
justObservable.subscribe(s -> {
//This will print the default value which is First Page
//Since the just create the observable even before the subscription
Log.d("RxJava Just",s);
});
deferObservable.subscribe(s -> {
//This will print the default value which is Second Page
//Since the defer wrapper wait till subscription before creating the observable
Log.d("RxJava Defer",s);
});;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment