假设您有以下Observable
内容rxjava-scala-0.18.4
@volatile var dorun = true
var subscriber: Subscriber[String] = null
val myObs = Observable { obs: Subscriber[String] =>
subscriber = obs
Subscription { println("unsubscribed"); dorun = false }
}
val sub = myObs.head.subscribe(println(_))
assertTrue(dorun)
subscriber.onNext("hello")
Thread.sleep(500)
assertFalse(dorun)
subscriber.onNext("world")
Thread.sleep(500)
assertFalse(dorun)
第二个断言失败,这意味着head
没有取消订阅。我对 Observables 的理解是错误的还是应该head
在第一个元素发出后取消订阅?