If a Subject
inherit from Observable
, whats the difference the next options based on any Subject
like:
private val locationSubject: ReplaySubject<Location> = ReplaySubject.create<Location>()
1. Returning a subject
itself as Observable
fun getLocations(): Observable<Location> = locationSubject
2. Returning subject.asObservable()
.
fun getLocations(): Observable<Location> = locationSubject.asObservable()
if you look at the implementation of .asObservable
you will see it lifts the observable with an operator that does nothing. This effectively just wraps your subject in an observable which makes it impossible for the consuming code to cast it back to a subject.
asObservable
is a defense mechanism to hide implementation details and not much else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With