The beauty of RxJava is that asynchronous operation is very convenient, that is, the use of Schedulers. This project will give you a thorough understanding of the use of Schedulers, common operators, RxAndroid… Detailed explanation of the use of.
Screenshots
Break down
We can think of Schedulers as thread controllers. There are five thread controllers that can switch between different threads. Here are the differences between the five thread controllers:
-
Schedulers.immediate() runs on the current thread, which is equivalent to not switching threads. This is the default Scheduler.
-
Schedulers.newthread () always starts a newThread and performs operations on the newThread.
Schedulers.io()
Scheduler used for I/O operations (reading and writing files, databases, network information interaction, and so on). The behavior of IO () is similar to that of newThread(), except that the internal implementation of IO () uses an unlimited pool of threads and can reuse idle threads, so in most cases IO () is more efficient than newThread(). Don’t put your calculations in IO () to avoid creating unnecessary threads.Schedulers.computation()
The Scheduler used for the calculation. This calculation refers to CPU intensive computing, that is, operations that do not limit performance by operations such as I/O, such as graphics computation. This Scheduler uses a fixed thread pool of CPU cores. Don’t focus on COMPUTATION (), or WAIT time for I/O operations will waste CPU.AndroidSchedulers.mainThread()
Switch to the main thread, and the specified operation will run on the Android main thread.
Actually we commonly used 2 kinds: Schedulers. IO () and AndroidSchedulers mainThread ()
The following examples use observable. just(1,2,3) to create the observed object, and the observer prints 1,2,3.
2. Set the observed thread using subscribeOn(schedulers.io ())
The following examples do not show which thread the observed is on. Using Observeble. Create () to create the observed can show which thread the observed is on, see other demos in the source code.
3. Set the observed and observer threads using subscribeOn(schedulers.io ()) and observeOn()
4. Use schedulers.io () to specify the thread on which the observed generates the event, and then use Map to transform the data by adding an ‘A’ after each data.
5. Use the Schedulers. IO () the specified event thread, observed using the Map of data conversion, behind each data to add ‘a’, use AndroidSchedulers. MainThread () to switch to the main thread, and then use the Map transformation, each data after add “b”, Output the result.
6. Use Schedulers. IO () the specified event thread, observed using the Map of data conversion, behind each data to add ‘a’, use AndroidSchedulers. MainThread () to switch to the main thread, and then use the Map transformation, each data after add “b”, Schedulers.io() is used to switch threads and add ‘C’ to data using Map to output the result.
7. What is different in this example is that subscribeOn() is used twice to specify the observed thread. Finally the top subscribeOn() works and the bottom subscribeOn() does not. Then use a Map of data conversion, here just at the back of each data to add ‘a’, use Schedulers. IO switch () thread, and then use the Map transformation, each data after add “b”, reoccupy AndroidSchedulers. MainThread () to switch the main thread, Add ‘C’ to the data using Map, and finally switch to the non-main thread to output the result.
It turns out that only the top thread of the observed thread is active, and the bottom thread is not.
See source code for more examples
About me
An android developer in Beijing.Welcome to offer me an Interview invitation. If you have any new idea about this project, feel free to contact me. 😃
License
Copyright 2016 Maat
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copy the code