0

When I switch from running the observeOn on the Main thread to a newThread, the onNext only runs once.

That only way I can get it to work is to keep it on the Main thread, but then I get that it does too much work on the Main Thread.

Without the setErrorHandler it just crashes (Also, can I use doOnError instead of RxJavaPlugins?)

PS. It works on the emulator fine, but it's on the physical device that the issue comes up.

public void updatePie() {

    RxJavaPlugins.setErrorHandler(Functions.<Throwable>emptyConsumer());

    Observable<Long> intervalObservable = Observable
            .interval(1, TimeUnit.SECONDS)
          //.doOnError(Functions.<Throwable>emptyConsumer())
            .subscribeOn(Schedulers.io())
            .takeWhile(new Predicate<Long>() {
                @Override
                public boolean test(Long aLong) throws Exception {

                    if (isMyServiceRunning(MyService.class) == false) {
                        RxB = false;
                    }
                    return RxB;
                }
            })
            .observeOn(Schedulers.newThread());

    intervalObservable.subscribe(new io.reactivex.Observer<Long>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Long aLong) {

            triple = mService.Time;
            entries.set(0, new PieEntry(mService.Time, "kronk"));
            entries.set(1, new PieEntry(mService.Time2, "notre dame"));
            pie_chart.notifyDataSetChanged();
            pie_chart.invalidate();

        }

        @Override
        public void onError(Throwable e) {
            Log.d(TAG, "Pie Update " + e.toString());
        }

        @Override
        public void onComplete() {
            Log.d(TAG, "Pie Update completed");
        }
    });
}
  • check here: https://stackoverflow.com/questions/33415881/retrofit-with-rxjava-schedulers-newthread-vs-schedulers-io maybe this will answer your question. – SkypeDogg Jun 04 '20 at 06:58
  • what's `pie_chart` ? – Blackbelt Jun 04 '20 at 07:07
  • @SkypeDogg the problem is that nothing works except for main thread – Crunchymind Jun 04 '20 at 07:25
  • @Blackbelt It's a variable of type PieChart. The PieChart in android studio – Crunchymind Jun 04 '20 at 07:26
  • Could you please specify your use-case in detail. What do you want the method to do and how is the actual behavior. Who is calling updatePie() and how? What is the expected behavior, when calling updatePie()? What happens in the subscribe? Do you change ui-elements or do you change domain state? – Sergej Isbrecht Jun 04 '20 at 14:41
  • @HansWurst On a button onclicklistener call the method which has the Observer, which basically retrieves entry set data in onNext and draw it on a pie chart. – Crunchymind Jun 04 '20 at 18:28
  • @Crunchymind, do I understand it correctly, that updatePie is called each time, a button is pressed? This would resulte in a memory-leak, because a subscription happons on each klick. The subscription will stay open, because it is a hard ref., which needs disposing. Furthermore is pie_chart a UI element? Android ui elements are only allowed to be called from the main-thread. In your example Observer#onNext would be called from a new thread. If a ui-element is called from a non main-thread an exception is thrown. But the subscription should stay open. – Sergej Isbrecht Jun 04 '20 at 19:48
  • @HansWurst It is an android UI, you see it works on the emulator though which is weird. Okay, how would I be able to run it on another thread? What is a work around. – Crunchymind Jun 04 '20 at 23:17

0 Answers0