1

I am trying to start Firebase jobdispatcher asynchronously using RxJava2.

@Override
    public boolean onStartJob(JobParameters job) {
    Completable.fromAction(new Action() {
            @Override
            public void run() throws Exception {
                startMethod();
            }
        }).subscribeOn(mSchedulerProvider.io())
          .observeOn(mSchedulerProvider.mainThread())
          .subscribeWith(new DisposableCompletableObserver() {
            @Override
            public void onComplete() {
                LOG.debug("onComplete");
                onStopJob(job);
            }

            @Override
            public void onError(Throwable e) {

            }
        });
        return true;
}

@Override
    public boolean onStopJob(JobParameters job) {
        LOG.debug("stop job");
        return true;
}

When i added subscribeOn(mSchedulerProvider.io()) line the startMethod() doesn't start, if I delete this line the startMethod() starts in Main thread.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Delphian
  • 1,650
  • 3
  • 15
  • 31
  • 1. What are the mSchedulerProvider.io()` and `mSchedulerProvider.mainThread()`. Why not use `Schedulers.io()` and `AndroidSchedulers.mainThread()` . 2. Is it a must to use RxJava – martinomburajr Jan 17 '18 at 13:50
  • 1
    1) It's the same. 2) What do you suggestion to use. I am using RxJava in project that is why i try to use it in this place. – Delphian Jan 17 '18 at 13:56
  • If I am correct, you want to know when the execution of the `startMethod()` on a different thread is complete? So you can call` onStopJob()`? – martinomburajr Jan 17 '18 at 13:57
  • 1
    Can I also get a code snippet of the `startMethod()` – martinomburajr Jan 17 '18 at 13:59
  • No. If I start with mSchedulerProvider.io() nothing work, I can't understand why dispatcher won't work with Schedulers.io(). – Delphian Jan 17 '18 at 14:00
  • startMethod is a big method, but i can do it empty it doesn't depend because the startMethod doesn't start at all. – Delphian Jan 17 '18 at 14:04
  • Please see if there is an `onError` callback and `printStackTrace` it. – akarnokd Jan 17 '18 at 21:54
  • I can't understand the problem because even an empty method works unstably, it works, then it doesn't. – Delphian Jan 18 '18 at 08:12

2 Answers2

1

I think your problem may be in the observeOn statement. I tried replicating the code you have. But since I assume you are not trying to update the UI thread, removing the observeOn will allow the process to finish on the RxCachedScheduler thread that the Schedulers.io() creates and will not block the UI

Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
            Log.e("RXJAVA","Running on | " + Thread.currentThread().getName());
            startMethod();
        }
    })
    .subscribeOn(Schedulers.io())
   // .observeOn(AndroidSchedulers.mainThread()) //Try comment this out
    //also try subscribe instead of subscribeWith
    .subscribe(new DisposableCompletableObserver() {
        @Override
        public void onComplete() {
            System.err.println("RXJAVA onComplete | " + Thread.currentThread().getName());
            Log.e("RXJAVA","onComplete | " + Thread.currentThread().getName());
        }

        @Override
        public void onError(Throwable e) {

        }
    });
martinomburajr
  • 1,235
  • 14
  • 29
1

I think the problem could potentially be from RxJava itself Try replacing your code with this. I would advise just use a simple Java threading primitive e.g. ExecutorService or CompletableFuture. Here's an example

Executors.newSingleThreadExecutor()
            .execute(new Runnable() {
                @Override
                public void run() {
                    startMethod();
                    onJobFinished(job); //job may need to be final;
                }
            });

If the following doesn't work, confirm that your Service is actually being called. Check your manifest that you have declared your service there. etc

martinomburajr
  • 1,235
  • 14
  • 29
  • Thank you for your sample. I changed the app using IntentService and all works perfect. I agree that the problem in RxJava. But a lot of people using RxJava and it's necessary to understand why there is a problem. I added this question to firebase github. – Delphian Jan 18 '18 at 13:14
  • Maybe do you know how to stop JobService from IntentService correctly? – Delphian Jan 18 '18 at 13:15
  • Hey Delphian, glad to have helped a bit. Not so sure how to stop JobService. I have used RxJava and sometimes I do get unexpected Behavior especially when I try using Threading. My rule of Thumb is if it involves Threading, I just use Plain Java Threading methods – martinomburajr Jan 18 '18 at 13:38