These are chat archives for ReactiveX/RxJava

21st
Oct 2015
David Stemmer
@weefbellington
Oct 21 2015 13:48
so, a Java-specific question. A class exposes a public static final Observable. Objects from other classes can subscribe to the static Observable. Is this an antipattern? It doesn't smell right to me, but I wanted a second opinion. Normally I'm very wary about memory leaks in these kinds of scenarios, and implications for unit testing.
David Stemmer
@weefbellington
Oct 21 2015 16:38
unrelated question:
it seems like that if an Exception is thrown in a Subscriber's onNext method, the Observable's doOnTerminate callbacks never fire
is there a way to get around this?
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Oct 21 2015 17:10
@weefbellington are you sure that you've subscribed to the Observable returned after applying doOnTerminate()?
David Stemmer
@weefbellington
Oct 21 2015 17:29
@artem-zinnatullin fairly certain, yes. What's happening in my case is that the source observable is emitting some data which comes from a remote API. The data returned by the API side may be malformed. If I try to do something with the malformed data in the onNext method, a RuntimeException may be thrown (NullPointerException, etc.). The behavior I'm seeing is that if a RuntimeExceptionis thrown in the subscriber onNext (or a doOnNext action), the onError callback is firing, but the doOnTerminate actions are not.
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Oct 21 2015 17:34
Looks like a bug, can you please post a failing test as issue to the RxJava repository? https://github.com/ReactiveX/RxJava
David Stemmer
@weefbellington
Oct 21 2015 17:35
@artem-zinnatullin I'll do that, thanks
Artem Zinnatullin :slowpoke:
@artem-zinnatullin
Oct 21 2015 17:36
:+1:
fAns1k
@fAns1k
Oct 21 2015 18:42
hi all, how to get data from some operation above, e.g.
.flatMap(data -> )
                    .map(data -> mapped)
                    .compose(RxUtil.applyApiSchedulers())
                    .subscribe(nextData -> {
//here i need to use data from flatMap
                               },
                            throwable -> {
                                Timber.e(throwable, "scan was fault");
                            }));
Dorus
@Dorus
Oct 21 2015 18:53
You need to keep the data alongside mapped.
fAns1k
@fAns1k
Oct 21 2015 18:54
save it outside the stream?
Dorus
@Dorus
Oct 21 2015 18:54
no
fAns1k
@fAns1k
Oct 21 2015 18:56
like create new object and push it further?
Dorus
@Dorus
Oct 21 2015 18:56
Simplest way would probably be a tuple or something like that, .map(data -> new tup(mapped, data)). Alternative is to use a internal stream there. I dont have the code for that at hand.
fAns1k
@fAns1k
Oct 21 2015 18:58
the second variant is more interesting, thanks. if you can share some code i would be appriciente
Dorus
@Dorus
Oct 21 2015 19:03
Do you still need mapped inside subscribe?
fAns1k
@fAns1k
Oct 21 2015 19:03
i need data (from snippet above) inside subscribe
but much more interesting solution itself )
David Stemmer
@weefbellington
Oct 21 2015 19:08
@artem-zinnatullin: ReactiveX/RxJava#3463
Dorus
@Dorus
Oct 21 2015 19:08
Something like this
.flatMap(data -> Observable.create(o -> {
        Observable.just(data)
        .map(data1 -> mapped)
        .compose(RxUtil.applyApiSchedulers())
        .map(__ -> data).subscribe(o);
    }))
David Stemmer
@weefbellington
Oct 21 2015 19:09
@Dorus if you have any thoughts on that issue I posted on the RxJava issue tracker I'd welcome your input as well
Dorus
@Dorus
Oct 21 2015 19:10
@weefbellington No clue. Looks like a bug to me indeed. Cant give you more than 'catch your own exceptions' for now
@fAns1k Not sure if that's the cleanest way, but it should do the job.
fAns1k
@fAns1k
Oct 21 2015 19:12
i can’t catch where the right brackets here
Dorus
@Dorus
Oct 21 2015 19:12
Yeah my editor converted tabs to spaces. Should look better now.
fAns1k
@fAns1k
Oct 21 2015 19:13
yep, that’s more readable, thanks. will try
Dorus
@Dorus
Oct 21 2015 19:14
I'm not even sure if the Observable.create is needed. Might work with only Observable.just...
fAns1k
@fAns1k
Oct 21 2015 19:15
subscription here is also need?
Dorus
@Dorus
Oct 21 2015 19:16
Yes, for the Observable.create. This is without Observable.create:
    .flatMap(data -> 
        Observable.just(data)
        .map(data1 -> mapped)
        .compose(RxUtil.applyApiSchedulers())
        .map(__ -> data)
    )
fAns1k
@fAns1k
Oct 21 2015 19:17
is there any way to use ‘mapped’ object inside result subscription, like data and mapped object is needed
Dorus
@Dorus
Oct 21 2015 19:18
You can use zip where i used map(__ -> data)
But you would still need a object that holds both data and mapped, or combine them in another meaningful way so subscribe can use it.
fAns1k
@fAns1k
Oct 21 2015 19:19
yep)
Dorus
@Dorus
Oct 21 2015 19:19
Actually no need for zip
just .map(mapped -> combine(data, mapped)) etc.
fAns1k
@fAns1k
Oct 21 2015 19:20
it’s like the same as first solution - to use map -> and create result object
Dorus
@Dorus
Oct 21 2015 19:20
only you inject mapped in the end
so no need to worry about it inbetween
unpacking tuples in every map etc.
fAns1k
@fAns1k
Oct 21 2015 19:22
thank you very much for your help
Dorus
@Dorus
Oct 21 2015 19:25
Sure, glad i was of (some) help.
@fAns1k : What you can do, is do your subscribe logic in the lat map and use a empty .subscribe() after the flatmap. Might be appropriate to use doOnEach. Perhaps only part of your logic needs to be there. Not my favorite solution but at least it's readable.
fAns1k
@fAns1k
Oct 21 2015 19:31
i need solution as readable as it can be, so i will play with it
Dorus
@Dorus
Oct 21 2015 19:32
I'm just throwing around some ideas :)
fAns1k
@fAns1k
Oct 21 2015 19:32
:D
Dorus
@Dorus
Oct 21 2015 19:33
I still have the feeling you're doing something wrong if you need data back in your subscription, so indeed something to think hard about and keep as readable as possible ;)
fAns1k
@fAns1k
Oct 21 2015 19:37
i get some data - create a stream - do requesti with data - get response - map - subscribe
and inside onNext i need to have data.getId() [from the first step], that is my current logic
Dorus
@Dorus
Oct 21 2015 19:41
Yeah so you need a wrapper that keeps data.getId() and the response. Not much against that other than performance. The extra object lives very short anyway.
fAns1k
@fAns1k
Oct 21 2015 19:42
yeah))
David Stemmer
@weefbellington
Oct 21 2015 20:05
got an answer on ReactiveX/RxJava#3463
the explanation feels a little funny to me as it seems like whether or not doOnTerminate is called after a downstream error is at the discretion of the operator
seems a little bit like a leaky abstraction to me, but c'est la vie