Let's talk briefly about the use and value of Rxjava
Use: asynchronous (ie, thread jump)
Value: In the face of complex logic, it is still concise , code easy to read
RXJAVA2 and Rxjava The biggest difference is flowable , this flowable replaces the simple use of Rxjava:
Here's a piece of code:
flowable. FromArray (weeks). Map (new function<string, string> () { @Override public string Apply (string s) throws Exception {print ("fromflowable map s =" + S
);
Return "111"; }). Flatmap (New function<string, flowable<string>> () {@Override Pub
Lic flowable<string> apply (String s) throws Exception {print ("fromflowable flatmap s =" + s);
string[] STRs = new string[]{"222", "333", "444"};
Return Flowable.fromarray (STRs); }). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (n
EW consumer<string> () {@Override public void accept (String s) throws Exception {
Print ("fromflowable subscribe s =" + s); }
});
Flowable
. Just ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
. Subscribe (New consumer<string> () {
@Override public
Void Accept (String s) throws Exception {
print ("jsutflowable subscribe s =" + s);
}
);
This is the simplest use of RxJava2. FromArray Method:
Pass in an array parameter. Inject the parameters into the flowable .
It's gone. Just pass the numbers in. Just method:
Pass in several of the same parameters and inject the parameters into the flowable .
No, just pass in 1 or more parameters.
The same as the FromArray () . Pass parameter !!! Map Method:
The map () method is also well understood.
1. flowable after passing the parameters
2. Then call map ()
3. map () in the callback, you can directly get the incoming parameters , and need to return a return value . To be followed up by the program.
4. Parameters can be arbitrarily manipulated conversion, parameters and the type of return value . generic control of Function
5. The first of the Function generics is the parameter that is passed in and the second is the type of the return value
the value of it:
The main purpose is to process the data. After processing, launch to the next level Flatmap method:
Flatmap () is all the same as the action usage of map () .
The only difference is in the return value type .
Flatmap returned to another flowable .
Actually publisher , because flowable is the implementation class for publisher , so at the moment pretending to return is flowable type
the value of it:
Personally, it's just less about writing a loop . Because often with Flatmap, return to the flowable , are constructed with FromArray () object. Meaning is to write less for Loop. Subscribe Method:
The Subscribe () method is to pass a Consumer object in. The official version of the object is the observed . A person understands that after a string of asynchronous operations, an operation is performed to echo the UI .
Write the code that echoes the UI inside the accept method. Subscribeon Method:
Subscribeon () incoming is a Scheduler object that is used to specify. which thread executes a series of operations
General incoming child threads are OK. Observeon Method:
Observeon () is a Scheduler object, used for Subscribeon , to specify the thread.
But the difference is:
Subscribeon () specifies which thread a sequence of operations executes on
Observeon () specifies which thread the subscribe callback executes on.
is generally subscribeon () child thread Execution Observeon () main thread execution foreign data simple show:
1. observable is the emitter of data, it will launch data, and then after the map, flatmap** and so on data processing, Eventually passed to **observer, the data receiver . Therefore, regardless of the intermediate data processing, we can see that theobservable data is the beginning of the data stream. Observer receive data, which is the end of the data stream .
The simple use of Rxjava, first written here
If you have any questions, please leave a comment.
I see that will be the first time to add.
For reference only, thank you for reading!