1

I have a variable called responses of type Observable[Try[Int]] that emits either:

Success(n), where n is a natural number or Failure(Exception)

I am summing the values of this observable like this:

val sum = responses.foldLeft(0) { (acc, tn) =>
  println("t1 tn: " + tn)
  println("t1 acc: " + acc)
  tn match {
    case Success(n) => acc + n
    case Failure(t) => acc // Failures are 0
  }
}

The print statements show this:

t1 tn: Success(1)
t1 acc: 0
t1 tn: Success(1)
t1 acc: 1
t1 tn: Success(1)
t1 acc: 2
t1 tn: Success(2)
t1 acc: 3
t1 tn: Failure(java.lang.Exception)
t1 acc: 5
t1 tn: Success(3)
t1 acc: 5
t1 tn: Success(3)
t1 acc: 8
t1 tn: Success(3)
t1 acc: 11

Immediately after, I am trying to get the sum from the observable like this:

var total = -1
val sub = sum.subscribe {
  s => {
    println("t1 s: " + s)
    total = s
  }
}

However, here, total is never updated and the print statement never shows anything.

Why is this the case?

Why are events not passed along any more?

bsky
  • 19,326
  • 49
  • 155
  • 270

0 Answers0