These are chat archives for ReactiveX/RxJava

12th
Jul 2018
blinkmacalahan
@blinkmacalahan
Jul 12 2018 04:22
Hi, I was wondering if anyone could help me with using Completables?
blinkmacalahan
@blinkmacalahan
Jul 12 2018 04:33
I have some logic where I need to perform a network request which has no result so I want to use Completable; however, this network request is sometimes optional based on a boolean value. How can I sometimes perform the network request and other times skip it? Note, this request is in a chain of other Completables.
Ignacio Baca Moreno-Torres
@ibaca
Jul 12 2018 05:30
You can use 'defer' and return a completed comoetable or the actual request. This is not the best approach because you will reference the boolean, so the defer won't be pure. Alternatively, if your boolean came from a Observable, you can use any combine operator (concatMap, flatMap or switchMap) and use the same empty or request strategy.
blinkmacalahan
@blinkmacalahan
Jul 12 2018 06:06
@ibaca, defer looks promising, but could you elaborate by the "defer won't be pure"? Are you referring to the fact the boolean value is not provided as a part of the stream and referenced from a "global" scope?
Ignacio Baca Moreno-Torres
@ibaca
Jul 12 2018 08:27
yep, defer forces to reference an external field to take the decision, so you better use some mapping to receive the event that you use to take the decision

so instead of

defer(() -> externalBoolean ? doNothing() : doSomething())

it is better to do…

map(internalBoolean -> internalBoolean ? doNothing() : doSomething())

also in this case, the “map” actually should be one of flatMap, concatMap, switchMap, etc; and the one you use will define how to handle multiple events, for example if this is a long request to read data, you’ll probably want to use the swithMap to cancel the previous request as soon as possible if it is still in progress

blinkmacalahan
@blinkmacalahan
Jul 12 2018 14:11
Looking at the docs, it doesn't appear that Completeable supports flatMap, concatMap, or switchMap. I guess I would need to switch to using an Observable or Single?
Ignacio Baca Moreno-Torres
@ibaca
Jul 12 2018 14:13
:+1:
where do you use this? what is the meaning of the boolean value?
blinkmacalahan
@blinkmacalahan
Jul 12 2018 14:34
During log in, I perform a series of network requests and other run time operations. If the user has recently logged in, I don't need to perform some of the requests or operations. The boolean tells me if I've recently logged in and don't need to perform the full set.
Ignacio Baca Moreno-Torres
@ibaca
Jul 12 2018 16:13
yep, but maybe it is even more clear if you just do something like...
class Session {
  Account current; boolean expired; 
  public Single<Account> account() { return expired ? load() : just(account); }
  private Single<Account> load() { …request, save and update expiration… }
}
Ignacio Baca Moreno-Torres
@ibaca
Jul 12 2018 16:19
not pretty at all, hehe maybe someone recommend something more functional… but if you are going to use the defer, just use a plain method instead
blinkmacalahan
@blinkmacalahan
Jul 12 2018 17:50
@ibaca Thanks for the suggestions!