read a article about scheduler <https://lorentzo...
# rx
f
read a article about scheduler https://lorentzos.com/i-bet-your-rxjava-is-on-the-wrong-thread-ae02e66a3eac
Copy code
fun printCurrentThread(tag: String) = println("$tag: ${Thread.currentThread().name.substringBefore("-")}")

fun main(args: Array<String>) {
	printCurrentThread("main thread name")

	val subject = BehaviorSubject.create(Unit)

	subject
			.subscribeOn(Schedulers.computation())
			.subscribe {
				printCurrentThread("subscribe onNext")
			}

	Thread.sleep(100L)

	subject.onNext(Unit)

	Thread.sleep(100L)
}

// The author said the code gives
   main thread name: main
   subscribe onNext: RxComputationScheduler
   subscribe onNext: main

The author explains:
Correct answer is A because the default value is based on the subscribeOn (computation thread) and .onNext() of the subject is called from the main thread. Therefore the second emission will ignore the subscribeOn, change thread and will be emitted at the main thread.
But from my running, it gives
Copy code
main thread name: main
subscribe onNext: main
my question is that is there really a
Where the first RxComputationScheduler onNext()
?
t
val subject = BehaviorSubject.create(Unit)
is bad syntax, it was probably meant to be
val subject = BehaviorSubject.create<Unit>()
which doesn’t put anything in it. meaning that if you subscribe before onNext is called there is nothing to deliver. if it had been
val subject = BehaviorSubject.createDefault(Unit)
you would have seen
Copy code
main thread name: main
subscribe onNext: computation*
subscribe onNext: main
f
Thanks, it makes sense now!
👍 1