Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make AndroidScheduler.mainThread work with a Scala Observable?

I have an Observable in my oncreate and I want to get the results on my main thread. But since The AndroidScheduler.mainThread works with just Java Observable, I am unable to compile it. Here's the code

val L=List(1,2,3)
val a=Observable.from(L).observeOn(AndroidSchedulers.mainThread()).subscribe(i=>println(i))

Here 'a' is a rx.scala.lang Observable. And this is the compile time error I am getting

 Error:(38, 70) type mismatch;
 found   : rx.Scheduler
 required: rx.lang.scala.Scheduler
 val a=Observable.from(L).observeOn(AndroidSchedulers.mainThread()).subscribe(i=>println(i))
                                                                 ^

Please help in solving this.

like image 550
user2730066 Avatar asked Dec 16 '14 18:12

user2730066


1 Answers

The JavaConversions class contains the method you need to convert from a "Java Scheduler" (the rx.Scheduler) to a "Scala Scheduler" (the rx.lang.scala.Scheduler) - javaSchedulerToScalaScheduler:

val a=Observable.from(L)
        .observeOn(JavaConversions.javaSchedulerToScalaScheduler(AndroidSchedulers.mainThread()))
        .subscribe(i=>println(i))
like image 124
Adam S Avatar answered Oct 23 '22 02:10

Adam S