dsafa
dsafa

Reputation: 793

Reactive Extensions (Rx) in Scala - execute a method after a given interval

Since I'm quite new to Reactive Extensions, I was curious about the following things.

By using Rx in Scala, I want to be able to call a method that retrieves content from an API every second.

So far, I've taken a look at the creational operators used within Rx such as Interval, Timer and so forth. But unfortunately, I cannot come up with the correct solution.

Does anyone have some experience with this, and preferably code examples to share?

Thanks in advance!

Upvotes: 2

Views: 98

Answers (1)

Malt
Malt

Reputation: 30285

Using RxJava:

Observable.interval(1, TimeUnit.SECONDS)
          .map(interval -> getSuffFromApi()) //Upto here, we have an observable the spits out the data from the API every second
          .subscribe(response-> System.out.println(response)); //Now we just subscribe to it

Or:

Observable.interval(1, TimeUnit.SECONDS) //Emit every second
          .subscribe(interval ->System.out.println(getSuffFromApi())) //onNext - get the data from the API and print it

Upvotes: 3

Related Questions