Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created August 17, 2018 16:46
Show Gist options
  • Save anitaa1990/b086dc832c6a3caeb27bf3547ec291bf to your computer and use it in GitHub Desktop.
Save anitaa1990/b086dc832c6a3caeb27bf3547ec291bf to your computer and use it in GitHub Desktop.
public class DeferClass {
private String value;
public void setValue(String value) {
this.value = value;
}
/*
* DeferClass instance = new DeferClass();
* Observable<String> value = instance.valueObservable();
* instance.setValue("Test Value");
* value.subscribe(System.out::println); //null will be printed
*
* Suppose we call the below class and create an Observable,
* when we call print, it will print null, since value had yet
* to be initialized when Observable.just() is called.
*/
public Observable<String> valueObservable() {
return Observable.just(value);
}
}
public class DeferClass {
private String value;
public void setValue(String value) {
this.value = value;
}
/*
* Now if we wrap the code inside
* Observable.defer(), none of the code inside of defer()
* is executed until subscription.
*/
public Observable<String> valueObservable() {
return Observable.defer(() -> Observable.just(value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment