These are chat archives for ReactiveX/RxJava

2nd
Sep 2016
Satyarth Sampath
@droidekas
Sep 02 2016 15:44

Hello, my problem is a bit extensive:
I have a composite subscription between my view and controller.The variables are behaviour subjects which I convert to observables use.

so

  public Observable<Contact> getContact() {
        return contact.asObservable();
    }

I use this observable to set my UI.
The problem is as such. the observable obtained from getName() is non-completing. When my fragment is paused/stopped ,I clear my CompositeSubscription and add all the subscribers again.This results in the last item of the observable to be reemitted.
I use the getName observable as a part of a click listener too. i.e.:

 compositeSubscription.add(RxView.clicks(contactView.email).filter(aVoid -> {
                Log.d(TAG, "clicked");
                return true;
            }).debounce(1000, TimeUnit.MILLISECONDS).subscribe(Void -> contactViewModel.sendEmail()));

where

public void sendEmail() {
        getContact().distinct().map(Contact::getEmail).subscribe
                (this::sendEmailInternal); // triggers a chooser intent
    }

As such on every pause/resume cycle,sendEmailInternal gets trigerred because of the value emitted.I understand this is how a BehaviourSubject well..behaves. But is there any way I can make sure that I can limit sendEmail() only to a click(i.e. one value emitted) and not continous changes to the observable.

Satyarth Sampath
@droidekas
Sep 02 2016 16:12
Hi,any help would really be appreciated,kinda stumped here
Dorus
@Dorus
Sep 02 2016 21:05
@droidekas Yeah, first of all, do not nest subscribe calls. If you solve that you probably have half the problem out of the way.
Second do not abuse filter for side effects. Use do
Third if you shouldn't need to track all your subscribers all day. You can also make a subscription that is on hold when the source is inactive, but you can also make one that unsubscribes itself when a certain condition is met. Like with takeWhile or takeUntil.
Fourth if you need to trigger emails only on click, the source should be the click event and nothing else. You can do something like clickEvent.withLatestFrom(otherSource)
That way you can still combine it with data from other sources.