Skip to content

Instantly share code, notes, and snippets.

@baldraider
Last active April 8, 2019 12:24
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 baldraider/00ff8d9c7851ca9cb2726c1dc04d3a7a to your computer and use it in GitHub Desktop.
Save baldraider/00ff8d9c7851ca9cb2726c1dc04d3a7a to your computer and use it in GitHub Desktop.
//In this example the value 'a' is written to the console:
fun BehaviorSubjectExample(){
//Need to provide a default value.
var subject = new BehaviorSubject<string>("a")
subject.Subscribe(println(it))
}
//In this example the value 'b' is written to the console, but not 'a'.
fun BehaviorSubjectExample2(){
var subject = new BehaviorSubject<string>("a")
subject.onNext("b")
subject.subscribe(println(it))
}
//In this example the values 'b', 'c' & 'd' are all written to the console, but again not 'a'
fun BehaviorSubjectExample3(){
var subject = new BehaviorSubject<string>("a")
subject.onNext("b")
subject.subscribe(println(it));
subject.onNext("c")
subject.onNext("d")
}
//Finally in this example, no values will be published as the sequence has completed. Nothing is written to the console.
fun BehaviorSubjectCompletedExample(){
var subject = new BehaviorSubject<string>("a")
subject.onNext("b")
subject.onNext("c")
subject.onCompleted()
subject.subscribe(println(it))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment