These are chat archives for ReactiveX/RxJava

19th
Sep 2016
Eugene Popovich
@httpdispatch
Sep 19 2016 07:23

I have the next problem using concat + subscribeOn(Schedulers.io())

Observable o1 = Observable.fromCallable(() -> {
Timber.d("Do something 1");
// some long running operation
return 1;
});

Observable o2 = Observable.fromCallable(() -> {
Timber.d("Do something 2");
// some long running operation
return 2;
});
Observable.concat(o1, o2).first()
.subscribeOn(Schedulers.io())
.subscribe(...)

I can see both records in the log for Do something 1 and Do something 2. I assume that is caused by subscribeOn(Schedulers.io()) because it may have more than one thread. Is there a possibility to overcome this like observeOn(Schedulers.io(), 1)?

Eugene Popovich
@httpdispatch
Sep 19 2016 08:25
Looks like that is my mistake in Retrofit Mock implementation. Question is resolved
Toon Sevrin
@toonsevrin
Sep 19 2016 16:23
Hey guys I would like to create an Observable that completes after a timeout, but the timeout is paused (not counting down) while a boolean is false. When this boolean turns true again, the timeout should continue.
Any idea how to do this? Currently I just have a loop that checks the value
Dorus
@Dorus
Sep 19 2016 16:35
@toonsevrin Well it might be easiest if you change the boolean into an observable. Even if not you can still do .timeout(() -> Observable.interval(1000).filter(checkBoolean).take(timeout).skip(timeout - 1)).
Toon Sevrin
@toonsevrin
Sep 19 2016 16:36
Already changed it to an observable (actually a behaviorsubject)
:)
hmm that's still pretty active with the interval checking
I was thinking of a pausable Scheduler
Dorus
@Dorus
Sep 19 2016 16:40
timeout(() => booleanStream.timestamp().scan(...).switchMap(time => time > 0 ? Observabe.timer(time) : Observable.empty()))
Something like that, and then calculate the timeout in scan.
Toon Sevrin
@toonsevrin
Sep 19 2016 16:41
What does .scan(...) do? :)
Dorus
@Dorus
Sep 19 2016 16:42
Yeah still need to come up with that :D
Toon Sevrin
@toonsevrin
Sep 19 2016 16:42
Aight i'll look all that stuff up and tell you how it went?
Dorus
@Dorus
Sep 19 2016 16:42
Sure, should be pretty straight forwards
Toon Sevrin
@toonsevrin
Sep 19 2016 16:43
scan would take the interval between the previous emission and the current emission right?
Dorus
@Dorus
Sep 19 2016 16:43
scan would calculate the remaining timeout
So if it get false, 0:00 it yields -1 but if it then gets true, 1:00 it'll yields 10 (hours) and then it'll get false, 2:00 it'll yield -1 again but remembers it has been going for 1 hour, so if it then gets true, 3:00 it'll yield 9.
So scan needs to both remember how long the stream has been running, when it has been put on pause (if so), and yield from that info how long the new timeout should be.
Toon Sevrin
@toonsevrin
Sep 19 2016 16:45
cool concept
let's see how it goes!
Dorus
@Dorus
Sep 19 2016 16:46
Shouldnt be more than a few lines of code to implement, and a small data object to keep all those values :)
Toon Sevrin
@toonsevrin
Sep 19 2016 16:46
Right after I get familiar with the operator ;)
Dorus
@Dorus
Sep 19 2016 16:48
Oh yes, i always start there too, and decent to the javadoc from there :)
Toon Sevrin
@toonsevrin
Sep 19 2016 16:48
Exactly, and into the source code when frustration arises haha
Dorus
@Dorus
Sep 19 2016 16:53
i usually write a small test query separated from my main program and play with that
or run it in a few unit testst i'll need anyway
Toon Sevrin
@toonsevrin
Sep 19 2016 16:56
Pen and paper all the way here :)
Toon Sevrin
@toonsevrin
Sep 19 2016 17:15
I think I got it down, but gotta go eat, thanks already!
Toon Sevrin
@toonsevrin
Sep 19 2016 20:13
All right I'm back and did some work
Doesn't seem like I need the scan
The timeoutfunction can do that behavior
Toon Sevrin
@toonsevrin
Sep 19 2016 20:20
Sorry for the lack of documentation, but here's my working solution: https://i.gyazo.com/8db5df582ad7d0908827405645f00740.png
Dorus
@Dorus
Sep 19 2016 21:07
@toonsevrin Line 48 can probably be removed as it's a duplicate of line 51.
Also you should probably take a Scheduler and use Scheduler.now() instead of Calendar
Dorus
@Dorus
Sep 19 2016 21:13
I am also unsure how this will pause/continue when your behavior subject yields. What variable holds this subject?
I do like your single function trick, do wonder if it'll work on multiple subscribers.