RxScala Observable永远不会运行

时间:2015-05-22 13:15:15

标签: scala rx-scala

使用以下demo_role | | {}

build.sbt

和这段代码:

name := "blah"

version := "1.0"

scalaVersion := "2.11.6"

libraryDependencies ++= Seq("io.reactivex" % "rxscala_2.11" % "0.24.1", "org.scalaj" %% "scalaj-http" % "1.1.4")

当我运行它时,所有打印的都是import rx.lang.scala.Observable import scala.concurrent.duration._ import scala.language.postfixOps object Main { def main(args: Array[String]): Unit = { println("Ready?") val o = Observable.interval(200 millis).take(5) o.subscribe(n => println(s"n = ${n}")) } } ;我完全看不到Ready?

我使用n = ...运行;它是使用Scala 2.6.11和RxScala 0.24.1以及sbt 0.13构建的。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

问题是您的程序会在o触发之前退出。请尝试以下代码:

import rx.lang.scala.Observable
import scala.concurrent.duration._
import scala.language.postfixOps
object Main {

  def main(args: Array[String]): Unit = {
    println("Ready?")
    val o = Observable.interval(200 millis).take(5)
    o.subscribe(n => println(s"n = ${n}"))

    Thread.sleep(5000)
  }

}

或者,您可以将Thread.sleep替换为o.toBlocking.lastoclass Intermediate{ private static Type myObject=null; public static void setIntermediate(Type ob){ myObject=ob; } public static String getValue(){ if(myObject!=null) return myObject.getValue(); else return ""; // <== always returning this value } } 终止之前无法返回。