First of all, I would like to emphasize that if you already know how to use this text, you can skip it and go back to the previous page.
preface
Maybe some people will ask, what age, still can’t RxJava? em… Yes, it’s not about ability, it’s a lot about environment. In particular, some companies will be more conservative in their technical requirements, or the person in charge of technology does not use this technology, so the students who just came out to work naturally do not know how to use it.
Therefore, for some students who know RxJava but never use it in project development, I hope to provide some help so that they can gradually use it in the project. After all, I don’t even know how to do it, let alone master it.
Of course, some people feel that there are so many excellent RxJava articles on the Internet, why write this?
em… I know that writing this kind of article is not going to generate many page views, but in the course of learning RxJava, I feel that most articles on RxJava are too focused on the principles of RxJava and its operators, people who know RxJava will find it very detailed, very in-depth, nice, like, like, like, like, But it’s easy for people who don’t know — I know the truth, but I just don’t know how to use it in practice.
So, virtually is a glass, understand the people will be more and more skilled, do not understand or in situ.
What I want to do is break the glass.
Let’s get back to the main text.
RxJava instructions
By convention:
Making address:
Github.com/ReactiveX/R…
configuration
Add dependencies:
Implementation "IO. Reactivex. Rxjava3: rxjava: 3.0.11." "Copy the code
Simple and easy to use
For the sake of explanation, Java is used again.
Observable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> emitter) throws Throwable { Log.e("TAG", "---> subscribe"); emitter.onNext(""); emitter.onNext(""); emitter.onComplete(); } }).subscribe(new Observer<Object>() { @Override public void onSubscribe(@NonNull Disposable d) { Log.e("TAG", "---> onSubscribe"); } @Override public void onNext(@NonNull Object o) { Log.e("TAG", "---> onNext"); } @Override public void onError(Throwable t) { Log.e("TAG", "---> onError"); } @Override public void onComplete() { Log.e("TAG", "---> onComplete"); }});Copy the code
Log output:
533-533/com.bjsdm.testrxjava E/TAG: ---> onSubscribe
533-533/com.bjsdm.testrxjava E/TAG: ---> subscribe
533-533/com.bjsdm.testrxjava E/TAG: ---> onNext
533-533/com.bjsdm.testrxjava E/TAG: ---> onNext
533-533/com.bjsdm.testrxjava E/TAG: ---> onComplete
Copy the code
A preliminary interpretation of the
This code is probably familiar to everyone, mainly divided into two parts:
- Create an Observable and call onNext() twice and onComplete() once.
- Create an Observer, override four methods, and discover that onNext() and onComplete() need to be actively called by Observable.
OK, that’s all we need to know, let’s look at how we normally write code.
Everyday code Examples
We need to implement a login feature.
First, we need to write out the login functionality as a utility class, and provide a login() method to call, and pass in a callback in the login() method to make it easier to call back if the server access succeeds.
LoginUtil.java
class LoginUtil { public void login(String name, String password, HttpCallBack HttpCallBack) {// Systemclock. sleep(1000); If (name.equals("BJSDM") && password.equals("123456")) {httpcallback.success (" login succeeded "); } else {httpcallback. error(" wrong account or password "); }}}Copy the code
HttpCallBack.java
interface HttpCallBack {
void success(Object object);
void error(Object object);
}
Copy the code
Calling code:
LoginUtil loginUtil = new LoginUtil();
loginUtil.login("BJSDM", "123456", new HttpCallBack() {
@Override
public void success(Object object) {
Log.e("login success:", object.toString());
}
@Override
public void error(Object object) {
Log.e("login error:", object.toString());
}
});
Copy the code
Log output:
4064-4064 / com. BJSDM. Testrxjava E/login success: : login successfulCopy the code
The code is not hard to understand, so I won’t elaborate on it, but we can summarize the main features briefly:
- Login () method call
- Callback method invocation
- The results show
Everyday code is merged into RxJava
First of all, the login() method is definitely in subscribe(), what? What is subscribe()?
Here it is:
Observable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> emitter) throws Throwable { Log.e("TAG", "---> subscribe"); emitter.onNext(""); emitter.onNext(""); emitter.onComplete(); }})Copy the code
You can call onNext() or onComplete(). You can also call onError(), for example:
Emitters. OnError (New Throwable);Copy the code
Thus, onNext(), onComplete(), and onError() are equivalent to callback methods, but onNext() is slightly different from onComplete() and onError(), which can only be called once, OnNext () can be called multiple times.
onComplete()
: indicates the completion of a processing result.onError()
: Indicates that the processing result is abnormal.onNext()
: Used to display data during service processing.onSubscribe()
: Used for service processing.
Of course, it can also be understood as the following correspondence:
onSubscribe()
As alogin()
onComplete()
As asuccess()
onError()
As aerror()
Based on what has been said above, we have compiled the following code:
Observable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> ObservableEmitter) throws Throwable {// Input String name = "BJSDM"; String password = "123456"; Systemclock. sleep(1000); if (name.equals("BJSDM") && password.equals("123456")) { emitter.onComplete(); } else {emitters. OnError (new Throwable); } } }).subscribe(new Observer<Object>() { @Override public void onSubscribe(@NonNull Disposable d) { } @Override public } @override public void onError(Throwable t) {log.e ("login error: ", t.toString()); } @override public void onComplete() {log.e ("login success: ", "onComplete()"); }});Copy the code
Operation effect:
4686-4686 / com. BJSDM. Testrxjava E/login success: : the onComplete ()Copy the code
Method execution order
It’s a lot of code, especially in Java, but the logic is clear.
It is basically executed in the following order:
onSubscribe()
: Operations performed before the service logic is executed.subscribe()
: Business logic code.onNext()
: Displays data during service processing.onComplete()
: The function is executed successfully.onError()
: The function fails to be executed.
Although the above statement is not rigorous enough, or even not accurate enough, but you can first understand the order in this way, learn to get started to practice, and so on the entry level, there will be opportunities to advance.
And through the above order, if we want to add a new function is very convenient, as long as the corresponding method in the code can be filled, for example, we want to add such a function:
- Make the button unclickable before you log in,
- Text is set to “Logging in” during network access.
- Make it clickable on success or failure, and change the text to “Login” :
Button btnClick = findViewById(R.id.btn_click); new Observer<Object>() { @Override public void onSubscribe(@NonNull Disposable d) { btnClick.setEnabled(false); } @override public void onNext(@nonnull Object Object) {btnclick.settext (" login "); } @Override public void onError(Throwable t) { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login error: ", t.tostring ()); } @Override public void onComplete() { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login success: ", "onComplete()"); }}Copy the code
Complete code:
Button btnClick = findViewById(R.id.btn_click); Observable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> ObservableEmitter) throws Throwable {// Input String name = "BJSDM"; String password = "123456"; Emitters. OnNext (" emitters "); Systemclock. sleep(1000); if (name.equals("BJSDM") && password.equals("123456")) { emitter.onComplete(); } else {emitters. OnError (new Throwable); } } }).subscribe(new Observer<Object>() { @Override public void onSubscribe(@NonNull Disposable d) { btnClick.setEnabled(false); } @override public void onNext(@nonnull Object Object) {btnclick.settext (" login "); } @Override public void onError(Throwable t) { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login error: ", t.tostring ()); } @Override public void onComplete() { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login success: ", "onComplete()"); }});Copy the code
Observables extract
Observable and Subscribe are stored separately because business logic processing is sometimes complicated:
LoginUtil.java
class LoginUtil { public Observable login(String name, String password) { return Observable.create(new ObservableOnSubscribe<Object>() { @Override public void Subscribe (@nonnull ObservableEmitter<Object> Emitter) throws Throwable {observable.onNext (" 中"); Systemclock. sleep(1000); if (name.equals("BJSDM") && password.equals("123456")) { emitter.onComplete(); } else {emitters. OnError (new Throwable); }}}); }}Copy the code
Business Code:
LoginUtil loginUtil = new LoginUtil(); Button btnClick = findViewById(R.id.btn_click); loginUtil.login("BJSDM", "123456").subscribe(new Observer<Object>() { @Override public void onSubscribe(@NonNull Disposable d) { btnClick.setEnabled(false); } @override public void onNext(@nonnull Object Object) {btnclick.settext (" login "); } @Override public void onError(Throwable t) { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login error: ", t.tostring ()); } @Override public void onComplete() { btnClick.setEnabled(true); BtnClick. SetText (" login "); Log.e("login success: ", "onComplete()"); }});Copy the code
Look, it looks a lot like Retrofit + RxJava code format.
Basically at this point, can understand, can start in the Demo, as for the use of various operators can see other articles.
Here, I just walk through the door, that’s all.