4

I have been trying to learn how to use RXJava, I was wondering how can I change the AsyncTask code below and use RXJava, is it possible. I am new to RXJava and since AsyncTask is being deprecated I wanted some help.

 private static class AddTeamAsyncTask extends AsyncTask<Team, Void, Void> {
        private TeamDao teamDao;
         AddTeamAsyncTask(TeamDao teamDao) {
            this.teamDao = teamDao;
        }

        @Override
        protected Void doInBackground(Team... teams) {
            teamDao.addTeam(teams[0]);
            return null;
        }
    }

2 Answers 2

8

RxJava is pretty straightforward. You could write that like this:

private void addTeamInBackground(Team team) {
    Observable.fromCallable(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            teamDao.addTeam(team);
            // RxJava does not accept null return value. Null will be treated as a failure.
            // So just make it return true.
            return true;
        }
    }) // Execute in IO thread, i.e. background thread.
        .subscribeOn(Schedulers.io())
        // report or post the result to main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // execute this RxJava
        .subscribe();
}

Or you can write it in Java 8 Lambda style:

private void addTeamInBackground(Team team) {
    Observable.fromCallable(() -> {
        teamDao.addTeam(team);
        // RxJava does not accept null return value. Null will be treated as a failure.
        // So just make it return true.
        return true;
    }) // Execute in IO thread, i.e. background thread.
        .subscribeOn(Schedulers.io())
        // report or post the result to main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // execute this RxJava
        .subscribe();
}

If you care about the result, you can add more callbacks into subscribe() method:

        .subscribe(new Observer<Boolean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Boolean success) {
                // on success. Called on main thread, as defined in .observeOn(AndroidSchedulers.mainThread())
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
Sign up to request clarification or add additional context in comments.

1 Comment

How can I use in a Sevice? Also, how can I dismiss/close the Observable?
0

I want to do network call using a method which return a boolean value

First: Import rxjava and rxandroid library: https://github.com/ReactiveX/RxAndroid

Then for single call:

new SingleFromCallable<>(new Callable<Boolean>() { //return type "Boolean" from network call
        @Override
        public Boolean call() {
            return URLIsReachable(searchText); //network call
        }
    })
            .subscribeOn(Schedulers.io()) //run in background thread
            .observeOn(AndroidSchedulers.mainThread()) //handle future response in ui thread
            .subscribe(new SingleObserver<Boolean>() { //start network call
                @Override
                public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {}

                @Override
                public void onSuccess(@io.reactivex.rxjava3.annotations.NonNull Boolean isValidUrl) {
                    loadingDialog.dismiss();
                    if(isValidUrl) //do future stuff on main thread
                }

                @Override
                public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                    loadingDialog.dismiss();
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.