I have just started with RxJava/android and for practice and getting started I want to make observable that emits character in string every 1 second, how can I do this? Here is what I have tried so far, its just emit string at once:
Observable<String> myObservable = Observable.interval(5000L, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.just("Hello");
I want hello like this first emit H after 1 second emit E and so on
Split String "hello" into letters "h", "e", ... "o" using
flatMap()
.new ArrayList<>(Arrays.asList(string.split(""))))
constructsList<String> letters
andObservable.fromIterable(letters)
emits each letter to downstream.Then
zipWith()
zips two Observables, one is letter, other is 1 second Time.Prints:
You can
zip
the interval with a character-producing source, such as theStringFlowable.characters()
extensions operator:Note that
just
is a static method that returns a new independentObservable
, thus calling.just()
on an instance method will have nothing to do with that previous flow.