Created
November 5, 2020 22:49
-
-
Save Nyame123/b8c70ecb346eb1ef94b3881aa97ee982 to your computer and use it in GitHub Desktop.
comparing defer with just usage
This file contains 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
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