These are chat archives for ReactiveX/RxJava

21st
Mar 2017
mreddimasi
@mreddimasi
Mar 21 2017 05:58
Hi,
I'm new to RxJava, I was wondering if it was possible to invoke a parallel CRUD operation just before return using RxJava?
Zak Taccardi
@ZakTaccardi
Mar 21 2017 06:01
Use schedulers to specify your threading
mreddimasi
@mreddimasi
Mar 21 2017 06:01
I have a service object where I return a CardResponse object which is a POJO, but before returning from the service, I would also like to insert into a table
the result of which I don't care about
Zak Taccardi
@ZakTaccardi
Mar 21 2017 06:02
In a fully reactive codebase, you should be thinking less about return values and more about emissions
Use .doOnNext() for side effects
mreddimasi
@mreddimasi
Mar 21 2017 06:04
@ZakTaccardi Thank you so much for your response.....so could you please help here....say I want to send CardResponse object back to the UI and also store the same in the DB
I should be doing something like <onCardResponseGenerated>.doOnNext()
Zak Taccardi
@ZakTaccardi
Mar 21 2017 06:07
Your UI should be observing a stream ofObservable<CardResponse>. In your data layer, that stream should be responsible for DB inserts
mreddimasi
@mreddimasi
Mar 21 2017 06:11
@ZakTaccardi Thanks again, the UI is built using Angular and is given a promise currently for CardResponse, but my question is more on the data layer where both a return of CardResponse and DB insert should happen in parallel.......so if I'm not wrong we should be using schedulers for the same
Zak Taccardi
@ZakTaccardi
Mar 21 2017 06:12
It should probably
Why parallel? Don't you mean you should be inserting the response in you DB?
mreddimasi
@mreddimasi
Mar 21 2017 06:17
@ZakTaccardi a controller is given an initial set of parameters like Card details, Merchant Id...etc, the controller calls a service object which in turn looksup the db and generates additional parameters that are required by a specific payment gateway and this is the CardResponse which needs to be returned to the UI and we also have to log the same in DB
so the UI doesn't have dependency to wait for the DB insert to happen
in the datalayer
Zak Taccardi
@ZakTaccardi
Mar 21 2017 06:21
I'm not sure exactly. You should set up a test environment for testing Rx behaviors
You could just call an asynchronous method in .doOnNext which would do the insert without blocking the stream
mreddimasi
@mreddimasi
Mar 21 2017 06:23
@ZakTaccardi ... Thanks so much for your help and patience, will definitely try your suggestions
Laurent
@Crystark
Mar 21 2017 11:30

Hi! I'm trying to get the max element from my observable. The thing is there can be more than one "max" element and i want to randomize the choice between those when it's the case. Currently i'm using rxjava-math and doing the following:

            .compose(o -> OperatorMinMax.max(o, (bp1, bp2) -> {
                int c = bp1.compareTo(bp2);
                return c == 0 ? (Randoms.R.nextBoolean() ? 1 : -1) : c; // Randomize order if equal
            }))

I'm breaking the comparator contract though even though it's done on purpose. Would there be a more friendly way of doing so ? I was thinking about reducing my elements to a list of all max elements and then shuffling them and taking first but it seems more complicated in terms of code and would probably be less perfomant.