preface

This article was originally prepared for internal training, but after some research, I found that my colleagues were not comfortable with RxJava, not because there were not enough “how to use” tutorials on the Internet, but because, At the beginning of the rush to start the tutorial countless, but never have a tutorial willing to use a few words to break the RxJava operator exactly what is sacred, why we use, why to use.

Observable.just(1.3.5.7.9)
    .map(i -> i + 1)
    .filter(i -> i < 5)
    .subscribe(getObserve());
Copy the code

In fact, for quite some time, I, like most people, only knew how to use RxJava for asynchronous callbacks, and I didn’t need to use any of the operators because I didn’t know what they were, and didn’t know how to use them.

Therefore, the purpose of this article is not to translate official documents and teach you how to use them, but to help you achieve an emotional understanding of RxJava operators. Given the good results of this training and the good feedback from my colleagues, I’ve opened up the full set of operator sample code on GitHub (don’t panic, the link is at the end of the article). If you’ve read this article and thought that’s what the RxJava operator is all about, then my wish has been fulfilled.

Programming languages contain multiple programming paradigms

My insight into the nature of operators began with my understanding of programming languages. Like you, I do Android development, but one day I decided to jump out of Java and try to learn and understand the nature of programming languages from a whole computer science perspective. In the process, I was introduced to the concept of “programming paradigms,” and learned that most programming languages contain multiple programming paradigms.

Common programming paradigms include imperative programming, declarative programming and so on.

In Java, for example, the dominant programming paradigm is imperative programming. Imperative programming is the sequential execution of specific commands that not only tell you what to do, but how to do it in detail at each step.

SQL is also a programming language, which is a typical form of declarative programming. Declarative programming is characterized by telling you what to do but not how to do it.

The nature of operators is declarative programming

Let’s go back to our original question. The reason you’re not comfortable with RxJava operators is because you’re used to using imperative programming thinking to understand operators that are actually declarative programming. Learning operators is just as natural as learning SQL statements in college.

SQL, as you understand it, is what you want to do to the data in the database according to certain rules.

In the same way, RxJava declares to the data stream what you want to do according to certain rules.

Converted to code, it looks like this.

Observable.just(1.3.5.7.9)
    .map(i -> i + 1)
    .filter(i -> i < 5)
    .subscribe(getObserve());
Copy the code

Does that make sense to you?

GitHub: RxJava Magician: Learn operators, just like you learn SQL!