Objective
A bit of a title party, in fact, no best practice. Some time ago the company implemented 996, so there is no time and effort to update the blog (ok I admit that I am lazy). So this article simply introduces the use of Rxjava in a production environment through two examples. But I have the complete code for each of the examples in this article.
The plan is to introduce the Rxjava framework and design ideas, but given that Netflix will release the RxJava2.0 official version by the end of October, it is decided to put the Rxjava framework structure and design thought analysis into the 2.0 official release. Later I will also have a series of articles to introduce the difference between rxjava1.x and 2.x.
Example one, get the app installed on your phone
The first example is that we need to display a list of installed third-party apps on our Android device, and I won't make a statement about the basics of environment building, dependency configuration, Recyclerview usage, and so on. Students who need to know can go to GitHub and take a look at the project clone. Here I mainly talk about how to achieve core functions through Rxjava.
First, we need to call the system API to get all the installed apps, so we call them in OnSubscribe
the call
method getApplicationInfoList()
. But getApplicationInfoList()
the data obtained does not fully meet our business needs:
- Because we only need to show the third-party app installed on the phone, we need to
filter
filter out the system app by operator;
ApplicationInfo
is not the type we need, so we need to map
convert it to by AppInfo
operator;
- Because acquiring
ApplicationInfo
, filtering, and transforming data is relatively time-consuming, subscribeOn
it is necessary to put this sequence of operations into a child thread through an operator;
- And to show the information on the page involves UI actions, so we need to pass the
observeOn
operator, onNext
onCompleted
onError
Dispatch to the main thread, and then we update the UI in these methods.
Here's the core code:
Final Packagemanager pm = mainactivity.This.getpackagemanager (); Observable.create (New Observable.onsubscribe<applicationinfo> () {@OverridePublicvoidPager(SUBSCRIBER<?Super Applicationinfo> subscriber) {list<applicationinfo> infolist = getapplicationinfolist (PM);for (ApplicationInfo info:infolist) {subscriber.onnext (info);} subscriber.oncompleted (); }}). filter (New Func1<applicationinfo, boolean> () {@OverridePublic BooleanPager(ApplicationInfo applicationinfo) {Return (Applicationinfo.flags & Applicationinfo.flag_system) <=0; }}). map (New Func1<applicationinfo, appinfo> () {@OverridePublic AppInfoPager(ApplicationInfo applicationinfo) {AppInfo info =New AppInfo (); Info.setappicon (Applicationinfo.loadicon (PM)); Info.setappname (Applicationinfo.loadlabel (PM). toString ());return info; }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (New Subscriber<appinfo> () {@Override public void oncompleted() {mapplistadapter.notifydatasetchanged (); Mpulldownsrl.setrefreshing (false);} @Override public void onError(Throwable e) {mpulldownsrl.setrefreshing (false);} @Override public void onNext(AppInfo AppInfo) {mappinfolist.add (AppInfo);}});
Program execution:
The complete code I put on GitHub, interested people can go to clone down to run their own look.
Source Address: Https://github.com/BaronZ88/HelloRxAndroid
Example two, rxjava+retrofit2 implementation get weather data
RxJava + Retrofit2 is almost standard for Android app development, and in this case we'll talk about how these two work together to help us develop quickly.
One of the standard interface definitions in RETROFIT2 is this:
@GET("weather")Observable<Weather> getWeather(@Query("cityId") String cityId);
Now with Rxjava, a basic network request can be implemented like this:
ApiClient.weatherService.getWeather(cityId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Weather>() { @Override public void call(Weather weather) { weatherView.displayWeatherInformation(weather); } });
But sometimes we may not know Cityid at first, we only know CityName. So we need to access the server first, get the corresponding city name of the Cityid, and then through this Cityid to get weather data.
Again, we need to define an interface to get Cityid:
@GET("city")Observable<String> getCityIdByName(@Query("cityName") String cityName);
Then we can use the omnipotent Rxjava to fulfill the demand.
ApiClient.weatherService.getCityIdByName ("Shanghai"). FlatMap (new func1<string, observable<weather>> () { @Override Public observable<weather> call(String Cityid) { return ApiClient.weatherService.getWe Ather (Cityid); }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (new Action1<weather > () { @Override public void call(Weather Weather) {weatherview.displayweatherinformation ( Weather); } });
Oh, whoa! ~ So easy!!! Mom doesn't have to worry about it anymore ....
Source Address: Https://github.com/BaronZ88/WeatherStyle
Weatherstyle the project is still under development, and the project includes not only the use of Rxjava and retrofit, but also the use of the Open source libraries of MVP, Ormlite, RETROLAMBDA, Butterknife, etc.
Rxjava1.x's series of articles to this end, because I have limited understanding of Rxjava, this series of articles if there are errors and please correct them. In the use of the Rxjava process have any questions and welcome everyone to communicate with me. Learn together! Progress together!
All right, let's meet RXJAVA2! ~
Links: https://www.jianshu.com/p/b0d8d3a5d3fc
Rxjava Series 7 (Best Practices)