0

I use playframework with "org.mongodb.scala" %% "mongo-scala-driver" % "1.0.1" and have that code:

  val collection = Mongo.db.getCollection("regionAuth")

  def getRegions = {

    val find: Observable[Document] = collection.find()

    Logger.info("regions searching")

    find.subscribe(new Observer[Document] {

      override def onError(e: Throwable): Unit = Logger.error("regions error", e)

      override def onSubscribe(subscription: Subscription): Unit = Logger.info("subscribed")

      override def onComplete(): Unit = Logger.info("regions done")

      override def onNext(result: Document): Unit = Logger.info("region accepted")
    })

    find.map { region =>
      Logger.info("region accepted by map")
      region
    }

    find.foreach(_ => Logger.info("region accepted by foreach"))

    find.toFuture().onComplete {
      case Success(r) => Logger.info("regions accepted as future seq " + r.size)
      case Failure(e) => Logger.error("regions error as future", e)
    }

    find
  }

And it prints:

[info] application - regions searching

[info] application - subscribed

[info] application - region accepted by foreach

[info] application - region accepted by foreach

[info] application - region accepted by foreach

[info] application - regions accepted as future seq 3

Why subscribe's events and map do not work?

Artem
  • 505
  • 5
  • 22

1 Answers1

0

I hope is not too late. If this is an stand alone application where nothing else is running, you must wait until the future completes.

...
val future = find.toFuture()
future.onComplete {
  case Success(r) => Logger.info("regions accepted as future seq " + r.size)
  case Failure(e) => Logger.error("regions error as future", e)
}

scala.concurrent.Await.result(future)
find
carlos_technogi
  • 198
  • 3
  • 6