我正在尝试学习 RxScala。我正在使用Observable.interval(1 second).buffer(10 seconds)
并订阅以打印值,但无法正确取消订阅。在我取消订阅后,它会继续运行,但结果为空。
我的代码:
import scala.language.postfixOps
import scala.concurrent.duration.DurationLong
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.Duration
import rx.lang.scala._
val o = Observable.interval(1 second).buffer(10 seconds)
val s = o.subscribe(
{ n => println("buffer --> " + n) },
e => { e.printStackTrace() },
() => { println("Completed!") })
它可以很好地打印结果:
buffer --> Buffer(0, 1, 2, 3, 4, 5, 6, 7, 8)
buffer --> Buffer(9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
buffer --> Buffer(19, 20, 21, 22, 23, 24, 25, 26, 27, 28)
buffer --> Buffer(29, 30, 31, 32, 33, 34, 35, 36, 37, 38)
一段时间后,我取消订阅:
s.unsubscribe
我仍然得到打印,但缓冲区为空:
buffer --> Buffer()
我认为这意味着我的订阅仍在运行,但接收到空缓冲区,我该如何停止订阅?