Created
August 17, 2018 16:46
-
-
Save anitaa1990/b086dc832c6a3caeb27bf3547ec291bf to your computer and use it in GitHub Desktop.
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
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