Skip to content

Instantly share code, notes, and snippets.

@vinaysshenoy
Last active March 27, 2018 08:54
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 vinaysshenoy/7d2389a43a09e658ffc1913087133599 to your computer and use it in GitHub Desktop.
Save vinaysshenoy/7d2389a43a09e658ffc1913087133599 to your computer and use it in GitHub Desktop.
Shared observable test
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit.SECONDS
import java.util.concurrent.atomic.AtomicInteger
val counter = AtomicInteger()
val source = Observable
.fromCallable {
println("\nCalled")
val number = counter.incrementAndGet()
if (number == 2) throw RuntimeException("test exception")
number
}
.delaySubscription(5L, SECONDS)
.subscribeOn(Schedulers.single())
.observeOn(Schedulers.single())
.doOnSubscribe { println("\nSubscribe source") }
.doOnDispose { println("\nDispose source") }
.doOnTerminate { println("\nTerminate source") }
.share()
.doOnSubscribe { println("\nSubscribe shared") }
.doOnDispose { println("\nDispose shared") }
.doOnTerminate { println("\nTerminate shared") }
source.subscribe({ println("\n#1: $it") }, { println("\n#1: Error")})
source.subscribe({ println("\n#2: $it") }, { println("\n#2: Error")})
Thread {
Thread.sleep(3000L)
source.subscribe({ println("\n#3: $it") }, { println("\n#3: Error")})
Thread.sleep(1000L)
source.subscribe({ println("\n#4: $it") }, { println("\n#4: Error")})
}.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment