Rxjava (i: Basic article)
Easy to Rxjava (ii: operator)
In layman's Rxjava three--the benefits of reactive formula
In layman's Rxjava four-use responsive programming in Android
What the hell is RxJava?
One word: async .
Where's RxJava?
In other words, "the same is done asynchronously, why do people use it instead of ready-made asynctask/handler/xxx/...?" 』
One word: concise .
The advantage of RxJava is simplicity, but its simplicity is unique in that it remains concise as the program logic becomes more complex.
(a) Basic use 1, create a observable object, directly call Observable.create can
[Java]View PlainCopy
- observable<string> myobservable = Observable.create (
- New Observable.onsubscribe<string> () {
- @Override
- public void Call (SUBSCRIBER<? Super String> Sub) {
- Sub.onnext ("Hello, world!");
- Sub.oncompleted ();
- }
- }
- );
The observable object defined here simply emits a Hello world string, and then it ends.
2, create a subscriber to handle the string emitted by the observable object.
[Java]View PlainCopy
- subscriber<string> mysubscriber = new subscriber<string> () {
- @Override
- public void OnNext (String s) {System.out.println (s);}
- @Override
- public void oncompleted () {}
- @Override
- public void OnError (Throwable e) {}
- };
Here subscriber just prints the string that observable sent.
3, we can associate our defined Myobservable object with the Mysubscriber object through the Subscribe function, which completes the subscriber to observable subscription.
Myobservable.subscribe (Mysubscriber); Once Mysubscriber subscribes to Myobservable,myobservable, the OnNext and OnComplete methods of the Mysubscriber object are called, and mysubscriber prints the Hello world! (ii) operator
Map: A mapping, transformation, used to convert an event to another event.
FLATMAP: Smooth map, receive a observable output as input, and output another observable.
Filter: filters, outputs and inputs the same elements, and filters out those that do not meet the check conditions.
Doonnext (): Do some extra things before you output one element at a time
Take: Outputs a maximum of a specified number of results.
... See the official API documentation for more operators.
In-depth theory: RxJava for Android developers
In layman Rxjava