How to run this instruction using RxJava?

The Rx way of doing things can be very complicated for no one, for many reasons ... but I feel that there are simple ways to do simple things with RX ...

How to simply execute this statement in the background thread and get a response to the ui thread?

All functions of this object must be performed in the background thread. Receive, place, clean and delete.

String city = Paper.get("city"); 
+5
source share
2 answers

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

What all this means is that you just need to define a class that wraps your call and passes the result to 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(); } } 

This is a basic example. Now you want to wrap this so that you can call any function, 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 this, allowing you to pass an arbitrary Callable .

What in your case will be implemented as:

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

(In case you are interested, this is java8 lambdas, which was added to android from retrolambda. It's very nice to remove the Rx verbosity)

Once you have the observable, you can subscribe to it and get the results. To execute in the background and get results in the ui thread, follow these steps:

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

AndroidSchedulers provided by rx-android .

Then you can simply call the callback with the result:

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

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

Generally:

 OperatorFromFunctionals.fromCallable(() -> Paper.get("city")) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(city -> Log.d(TAG, city)); 
+5
source

EDIT: This is incorrect. Do not delete the answer, but save the comments.

A very simple example:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getPaper() .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<String>() { @Override public void call(String s) { Log.d("xxx", s); } }); } private Observable<String> getPaper() { return Observable.just(Paper.get()); } 

where Paper.get() is a long operation that returns a String . Check the documents for the Scheduler .

Remember to watch the main thread if you want to change the user interface after receiving the result of your operation, otherwise you will get an exception for changing the user interface from outside the user interface.

0
source

All Articles