Last active
March 27, 2018 08:54
-
-
Save vinaysshenoy/7d2389a43a09e658ffc1913087133599 to your computer and use it in GitHub Desktop.
Shared observable test
This file contains hidden or 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
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