0

I'm very new to Java, android and Rxjava. I recently noticed that in an existing project (not written by me) a chat notification that is supposed to be received isn't received. Thus I started to do some tracing. Below is part of the code.

Note: Notifications that do get received seems to always go to onSuccess in the file FCMServices

I've put breakpoints pretty much everywhere in the code below. What I noticed was for the notifications that I do not receive onSuccess and onError do not get called but onComplete does. However I find that strange as I thought either onSuccess or onError must be called before onComplete.

My understanding of those functions is based on http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/MaybeObserver.html

//FCMService.java
currentConversationRepo.getCurrentConversation()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new MaybeObserver<CurrentConversation>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            currentChatDisposable = d;
                        }

                        @Override
                        public void onSuccess(@NonNull CurrentConversation currentConversation) {
                            System.out.println("This is SUCCESS");
                            if (channelSid == null && author == null && usedAdId == null){
                                buildNotifyNotification(body, action, "", userId);
                            }

                            if (channelSid != null && author != null) {
                                if (!channelSid.equals(currentConversation.getChannelSid())) {
                                    createChatNotification(author, channelSid, body);
                                }
                            }
                            currentChatDisposable.dispose();
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            System.out.println("Error getting current conversation: " + e.getMessage());
                            currentChatDisposable.dispose();
                        }

                        @Override
                        public void onComplete() {
                            System.out.println("This is onComplete");
                            currentChatDisposable.dispose();
                        }
                    });

I then started to do some tracing of where onComplete was called and appears that it was called by another onSuccess from the class TestObserver in reactivex.io

http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/observers/TestObserver.html

//TestObserver.java
@Override
public void onSuccess(T value) {
    onNext(value);
    onComplete();
}

Which was in turn called by the onSuccess in MaybeFlatMapBiSelector class. (Also a reactivex.io class I believe)

//MaybeFlatMapBiSSelector.java
            @Override
            public void onSuccess(U value) {
                T t = this.value;
                this.value = null;

                R r;

                try {
                    r = ObjectHelper.requireNonNull(resultSelector.apply(t, value), "The resultSelector returned a null value");
                } catch (Throwable ex) {
                    Exceptions.throwIfFatal(ex);
                    actual.onError(ex);
                    return;
                }

                actual.onSuccess(r);
            }

This turned out to be from the MaybeObserver interface

http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/MaybeObserver.html#onComplete--

My question is what exactly are the onSuccess of TestObserver and MaybeFlatMapBiSelector doing? And if it is even possible based on the information I have provided, why is it that some notifications goes to onComplete without going to onSuccess or onError in FCMServices.java

Mark
  • 3,138
  • 5
  • 19
  • 36
  • As the documentation of MaybeObserver you linked states, it does not call onComplete after onSuccess ever. Also is the code you inherited using TestObserver in non-test code? It shouldn't. The TestObserver::onSuccess implementation does a protocol conversion to Observable so that Maybes, Singles and Completables can be tested with the same TestObserver class. You also refer to the MaybeFlatMapBiSSelector. Does the code not show use the two-arg flatMap? – akarnokd Jan 28 '20 at 08:49

1 Answers1

1

Have you tried to comment currentChatDisposable.dispose(); ? I've had the same issue not long ago where I was disposing of my disposable too early and no data where showing

Usually you call .dispose() when onPause() or onDestroy() of the lifecycle

PS: In case you didn't know Maybe in RxJava return either a single value, nothing at all or an exception.

Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • Thanks for the answer. I tried your suggestion but still no chat notifications. The only spot that creates notification is in ```onSuccess```. Since the breakpoint never stops there I think that is a problem. Just not sure why it does sometimes and not in other cases. Thanks for the explanation on ```Maybe```, been doing research on that for ages. – Mark Jan 27 '20 at 21:56
  • Have you checked the answer of your retrofit call ? Is it a 200 ? Is the object you're expecting parse successfully ? Rxjava might react like this because you've got error in your API call. You might want to add the okhttp logging-interceptor to see what you send, and what you receive. [example logging-interceptor](https://stackoverflow.com/questions/32514410/logging-with-retrofit-2) – Biscuit Jan 28 '20 at 09:04