Rxandroid
As a model in the design of the MVP can play the most thorough framework not to learn to feel really sorry for yourself, then also learn something new, responsive programming, MVP Observer mode, and then use rxandroid to make our own code more concise
Then read an article yesterday that the interface calls are frequently used to make your entire code look very difficult to understand, in fact, a reader to see your code, and then found that your code is flooded with interface callbacks, hey, this time
He's going to need to find out where this interface is going to go (think of it as well);
So the RX (Android, Java) series is a good solution to this problem, and I realized it yesterday, because you can put the tasks and tasks that you need to perform in the same place, and then you don't need to find them.
At a glance, the task (time-consuming task or time-consuming task) executes, the task return value and subsequent processing are called in a chain form;
What's the MVP ?
Which is the model view Presenter,
MVP mode separates the display layer and the logic layer, and communicates between them through the interface (as if many of my previous interfaces were communicating with the interface!) Also useful to MVP mode), reduce the coupling;
You can see that presenter interacts with model and view, and there is no direct link between view and model
So as our business expands and then the UI changes, or the business logic changes the UI, it can be a headache if you don't separate, because you want both, but now you just need to
Focus on UI or business logic changes! Well, that's the good thing about MVP.
And then the rxandroid.
Rxandroid is an extension of the Rxjava, and then there are many ways to work with the task
such as just,map,fromcallable and so on. Then there are two important classes observable (event Publisher, observer), OBSERVER (Observer, event Subscriber, Subscription (event subscriber).
The Just method uses
The just method is suitable for tasks that do not take time, synchronous methods
//Event PublishersObservable<string>listobservable;/**just test, where the getstring is a simple function that returns a string (not posted)*/listobservable=Observable.just (getString ()); Listobservable.subscribe (NewObserver<string>() {@Override Public voidoncompleted () {LOG.V ("Test", "oncompleted"); } @Override Public voidOnError (Throwable e) {log.v ("Test", "OnError" +e); } @Override Public voidOnNext (String s) {textview.settext (s); LOG.V ("Test", "OnNext:------" +1); } });
As in the code just () method parameter, a value can be passed in, and the type of the value is defined by the event Publisher and should be consistent with it. When the value is successful, the OnNext function is notified of "publish."
Then we can work on the OnNext function. This is simple to use, however the essence of rxandroid is in the operation of the thread (though I would say, but the feeling is still not fully understood)
Fromcallable () method
The Just method is only a basic approach, but in reality we have a lot of time-consuming operations such as network requests, such as database storage, and so on, we need to use asynchronous methods
Fromcallbale usage is similar to just, but it's more of a thread-control thing.
/**long time consuming task testing*/listobservable= Observable.fromcallable (NewCallable<string>() {@Override PublicString Call ()throwsException {returnGetstringlongtime (); } }); Subscription mtvsubscription=listobservable. Subscribeon (Schedulers.io ())//Specifies the thread that the method in observable runs. Observeon (Androidschedulers.mainthread ())//Specify OnNext Run thread. Subscribe (NewObserver<string>() {@Override Public voidoncompleted () {LOG.V ("Test_callable", "oncompled:------"); } @Override Public voidOnError (Throwable e) {log.v ("Test_callable", "OnError:------" +e); } @Override Public voidOnNext (String s) {textview.settext (s); LOG.V ("Test_callable", "OnNext:------" +1); } });
The observer new has a callable instance, which runs a long time-consuming task and asynchronously notifies the Observer's OnNext method when completed
Subscribon is a thread that specifies observable to run
Observeon is the thread that the specified observer OnNext method executes
Map method
Copy the code of the Great God here, express the use of map, self-understanding debounce set delay Time
Mtextwatchsubscription =msearchresultssubject. Debounce (, Timeunit.milliseconds)//set 400 millisecond wait time. Observeon (Schedulers.io ()). Map (NewFunc1<string, list<string>>() {@Override PublicList<string>Call (String s) {returnmrestclient.searchforcity (s); }}). Observeon (Androidschedulers.mainthread ()). Subscribe (NewObserver<list<string>>() {@Override Public voidoncompleted () {} @Override Public voidOnError (Throwable e) {} @Override Public voidOnNext (list<string>cities) {Handlesearchresults (cities); }}); Msearchinput.addtextchangedlistener (NewTextwatcher () {@Override Public voidBeforetextchanged (Charsequence S,intStartintCountintAfter ) {} @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) { //Call the OnNext method when there is a text changeMsearchresultssubject.onnext (s.tostring ()); } @Override Public voidaftertextchanged (Editable s) {}});
Photo Quote from: Android source design mode analysis and actual combat "He Honghui, Guan"
Part of the code copy from the blog park has not yet felt young
Rxandroid/java Xiao Kee