why does doOnComplete execute immedeatily regardless of delay() - rx-java2

I was just writing some sample code with takeUntil -
final Observable<Integer> stopper = Observable.just(1)
.doOnComplete(() -> view.append("second stream complete"))
.delay(500, TimeUnit.MILLISECONDS);
return Observable
.range(0, 10)
.zipWith(Observable.interval(100, TimeUnit.MILLISECONDS), (item, interval) -> item)
.takeUntil(stopper)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(view::append);
So ideally, the stopper emits after 500ms and terminates the second observable, which it does. BUT the doOnComplete prints immediately.
As I understand from the documentation - delay shifts the events forward in time - including the complete event. So why would this happen?

If you look in the source code of the just (namely ScalarDisposable in ObservableScalarXMap.java) operator you will see that all it does is emits one onNext event with the provided value and immediately after that it emits onComplete event. In your example you've put doOnComplete before delay operator - that's why doOnComplete gets called immediately and after that the event is delayed for 500ms.

According to documentation, Observable.just() completes immediately - so message will be printed out right now.
If you want the message be printed after delay you should modify it like that
Observable.just(1)
.delay(500, TimeUnit.MILLISECONDS)
.doOnComplete(() -> view.append("second stream complete"))

Related

Why onComplete not fired when an infinite stream is switchMap'ed

Using RxJava 2.2.8:
Observable.fromCallable(() -> "Some data")
.subscribe(
s -> System.out.println(s),
e -> System.err.println(e),
() -> System.out.println("Completed")
);
Output
Some data
Completed
My question is why onComplete never gets called for the following?
Observable.interval(1, TimeUnit.SECONDS)
.switchMap(t -> Observable.fromCallable(() -> "Some data"))
.subscribe(
s -> System.out.println(s),
e -> System.err.println(e),
() -> System.out.println("Completed")
);
Output
Some data
Some data
Some data
...
I understand Observable.interval will create a never ending stream, so no onComplete. My understanding of switchMap is that it returns an observable which fires events produced by the inner observable (cancelling any pending and flattening), in this case Observable.fromCallable.
Now, this 'inner' observable does have a definite end (unlike the outer observable), so why doesn't onComplete gets called on this inner Observable?
Why isn't the output like this?
Some data
Completed
Some data
Completed
Some data
Completed
...
From documentation:
The resulting ObservableSource completes if both the upstream
ObservableSource and the last inner ObservableSource
Since upstream ObservableSource is an infinite stream, the resulting Observable will not complete.
Also note that according to the observable contract, onComplete indicates the observable has terminated and it will not emit any further items in the future, so you will never see "Completed" followed by some other items regardless of your implementation.

How can I apply a grace time using RX?

I have an Observable<Bool> that emits true when an operation begins and false when it ends. I'd like to show a message while the operation is in progress, but only if it takes longer than two seconds to begin. Is there a way I can create an observable that I can bind my message to? Any help much appreciated!
If you switchMap (a flatMap where when a second item is emitted from the source the subscription to the original observable is unsubscribed and the subscription moves to the next) you could do something like this:
booleanObservable
.switchMap ( map true to an observable timer of 2 seconds, map false to an empty observable)
.onNext show your message (next won't fire for the empty and a
quick response would have cut off the 2 second timer).
Note switchMap is 'switchLatest' in RxSwift.
Could become something like this:
booleanObservable
.map { inProgress -> Observable<Bool> in
if inProgress {
return Observable.just(true).delay(time: 2)
} else {
return Observable.just(false)
}
}
.switchLatest()

How do I sequentially loop an observable in RxSwift?

I am trying to create a stream that polls a network service. At the moment it queries the service then completes after a short delay. I'd like the onward stream to restart rather than completing thereby polling the service forever.
You could do something like ...
myPollingStream.repeat()
But repeat in RxSwift is actually repeatElement and so actually generates a stream of observables. You could possibly concatMap these into a flattened serial sequence but RxSwift does not have the concatMap operator.
So how do I loop an observable in RxSwift?
I'd like the requests to be sequential, not concurrent so flatMap is not an option since it merges streams leading to overlapping requests. I'm looking for something similar to how retry() works but restarting onComplete not onError
Observable.repeatElement(myPollingStream, scheduler: MainScheduler.instance).concat()
repeatElement(_:scheduler:) will create an infinite stream of polling queries.
contat() will then make sure each polling query is completed before subscribing to the next.
Attention
While the above works in theory, without a backpressure implemetation, repeatElements(_:scheduler:) will emit events until you eventually run out of memory. This makes this solution not viable as of RxSwift 3.0. More details can be found in this issue on RxSwift repository.
Option 1: Recursive function
Your myPollingStream:
func myPollingStream() -> Observable<Result> {
return Observable<String>.create { observer in
// your network code here
return Disposables.create()
}
}
Then you create a a recursive function:
func callMyPollingStream() {
myPollingStream()
.subscribe(onNext: { result in
callMyPollingStream() // when onNext or onCompleted, call it again
})
.addDisposableTo(db)
}
Option 2: Use interval
let _ = Observable<Int>
.interval(5, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
let _ = myPollingStream().subscribe()
})
.addDisposableTo(db)
With this option, myPollingStream() function will be called every 5 seconds.

Observable windowed groupBy leads to OutOfMemoryError

I'm trying to figure out how to use Observable.groupBy to limit the number of elements pushed by key over a time frame. I end up with the following construct:
create(emitter -> {
while (true) {
publishedMeter.mark();
emitter.onNext(new Object());
}
})
.window(1000L, TimeUnit.MILLISECONDS)
.flatMap(window -> window.groupBy(o -> o.hashCode() % 10_000).flatMapMaybe(Observable::lastElement))
.subscribe(e -> receivedMeter.mark());
While subscribe's onNext callback is called a few thousand times, which I think should mean that flatMapMaybe does properly subscribe to all GroupedObservableSource. After a short while one of the thread inside RxComputationThreadPool but I don't understand what I'm missing

rx reactive extension: how to have each subscriber get a different value (the next one) from an observable?

Using reactive extension, it is easy to subscribe 2 times to the same observable.
When a new value is available in the observable, both subscribers are called with this same value.
Is there a way to have each subscriber get a different value (the next one) from this observable ?
Ex of what i'm after:
source sequence: [1,2,3,4,5,...] (infinite)
The source is constantly adding new items at an unknown rate.
I'm trying to execute a lenghty async action for each item using N subscribers.
1st subscriber: 1,2,4,...
2nd subscriber: 3,5,...
...
or
1st subscriber: 1,3,...
2nd subscriber: 2,4,5,...
...
or
1st subscriber: 1,3,5,...
2nd subscriber: 2,4,6,...
I would agree with Asti.
You could use Rx to populate a Queue (Blocking Collection) and then have competing consumers read from the queue. This way if one process was for some reason faster it could pick up the next item potentially before the other consumer if it was still busy.
However, if you want to do it, against good advice :), then you could just use the Select operator that will provide you with the index of each element. You can then pass that down to your subscribers and they can fiter on a modulus. (Yuck! Leaky abstractions, magic numbers, potentially blocking, potentiall side effects to the source sequence etc)
var source = Obserservable.Interval(1.Seconds())
.Select((i,element)=>{new Index=i, Element=element});
var subscription1 = source.Where(x=>x.Index%2==0).Subscribe(x=>DoWithThing1(x.Element));
var subscription2 = source.Where(x=>x.Index%2==1).Subscribe(x=>DoWithThing2(x.Element));
Also remember that the work done on the OnNext handler if it is blocking will still block the scheduler that it is on. This could affect the speed of your source/producer. Another reason why Asti's answer is a better option.
Ask if that is not clear :-)
How about:
IObservable<TRet> SomeLengthyOperation(T input)
{
return Observable.Defer(() => Observable.Start(() => {
return someCalculatedValueThatTookALongTime;
}, Scheduler.TaskPoolScheduler));
}
someObservableSource
.SelectMany(x => SomeLengthyOperation(input))
.Subscribe(x => Console.WriteLine("The result was {0}", x);
You can even limit the number of concurrent operations:
someObservableSource
.Select(x => SomeLengthyOperation(input))
.Merge(4 /* at a time */)
.Subscribe(x => Console.WriteLine("The result was {0}", x);
It's important for the Merge(4) to work, that the Observable returned by SomeLengthyOperation be a Cold Observable, which is what the Defer does here - it makes the Observable.Start not happen until someone Subscribes.