Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I run this statement using RxJava?

Rx way of doing things can be very complex for none and for many reasons... but I feel there ARE simple ways to do simple things with RX...

How would I simply perform this statement on a background thread and receive the response on the ui thread?

All functions of this object need to run on a background thread. Get, put, clear, and delete.

 String city = Paper.get("city");
like image 585
sirvon Avatar asked Aug 21 '15 19:08

sirvon


People also ask

How do you use a single RxJava?

The Single class represents the single value response. Single observable can only emit either a single successful value or an error. It does not emit onComplete event.

What is RxJava and why to use it?

RxJava is a Java library that enables Functional Reactive Programming in Android development. It raises the level of abstraction around threading in order to simplify the implementation of complex concurrent behavior.

What is RxJava in Android example?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.


1 Answers

The base object in Rx is Observable. That object usually wraps an OnSubscribe object, which is simply an extension of Action1 that takes a Subscriber as a parameter.

What all that means is that you just need to define a class that wraps your call and passes the result to the Subscriber:

public class RxPaperGet implements Observable.OnSubscribe<String> {
    @Override
    public void call(Subscriber<? super String> t1) {
        try {
            t1.onNext(Paper.get("city"));
        } catch (Throwable t) {
            t1.onError(t);
            return;
        }
        t1.onCompleted();
    }
}

That's a basic example. Now, you would want to wrap that so you can call any function, and not just Paper.get("city"). Something like https://github.com/ReactiveX/RxJavaAsyncUtil/blob/0.x/src/main/java/rx/util/async/operators/OperatorFromFunctionals.java#L44 does that, by allowing you to pass an arbitrary Callable.

Which in your case, would implement as:

Observable<String> res = OperatorFromFunctionals.fromCallable(() -> Paper.get("city"));

(In case you're wondering, this is java8 lambdas brought to android by retrolambda. quite nice to remove the verbosity of Rx)

Once you have your observable, you can subscribe on it, and get results. To execute on the background, and retrieve the results on the ui thread, you would do:

 res.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())

AndroidSchedulers is provided by rx-android.

Then you can simply be calledback with the result:

.subscribe(city -> Log.d(TAG, city));

That returns a subscription, which is useful if you need to cancel it.

Overall:

OperatorFromFunctionals.fromCallable(() -> Paper.get("city"))
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(city -> Log.d(TAG, city));
like image 171
njzk2 Avatar answered Sep 18 '22 03:09

njzk2