RX Series Five | Schedulers Line Program Control system
In our last article, we have a code in our small example
//网络访问.observeOn(Schedulers.io())
In fact, when we use the network operation, we can control which thread it runs on, and the Schedulers class, there are four methods, namely
- Schedulers.immediate ();
- Schedulers.newthread ();
- Schedulers.io ();
- Schedulers.computation ();
And the androidschedulers in Rxandroid have a
- Androidschedulers.mainthread ();
These are the methods of threading, and we all know that Rxjava is excellent in terms of async, so how do we manifest him, first look at the specific meanings of these functions?
Schedulers.immediate ()
Runs on the current thread, which is the same thread on which thread you run the code on
Schedulers.newthread ();
Runs in a new thread, which is equivalent to the new thread (), and each execution is
Schedulers.io ();
At a glance, I/O operations, such as file operations, network operations, and so on, he and newthread a bit similar, but I/O there is a dead loop thread pool to achieve maximum processing logic, although high efficiency, but if it is a general calculation operation, not recommended here, avoid repeatedly creating useless threads
Schedulers.computation ()
Some operations that require CPU computing, shapes, videos, etc.
Androidschedulers.mainthread ();
Specify to run in the main Android thread
Let's simulate this operation now, like we're going to load a picture, so if we're using okhttp, what the code should say
@Override Public void OnClick(FinalView v) {Switch(V.getid ()) { CaseR.id.load_img:observable.create (NewObservableonsubscribe<bitmap> () {//main thread execution @Override Public void Subscribe(FinalObservableemitter<bitmap> E1)throwsException {//Perform network operation resolution pictureOkhttpclient client =NewOkhttpclient (); Request Request =NewRequest.builder (). URL (img_url). build ();//Async methodClient.newcall (Request). Enqueue (NewCallback () {@Override Public void onfailure(Call call, IOException e) {E1.onerror (e); }@Override Public void Onresponse(Call call, Response Response)throwsIOException {if(Response.issuccessful ()) {byte[] bytes = response.body (). bytes (); Bitmap Bitmap = Bitmapfactory.decodebytearray (Bytes,0, bytes.length); E1.onnext (bitmap); E1.oncomplete (); } } }); }}). Subscribe (NewObserver<bitmap> () {@Override Public void Onsubscribe(Disposable D) {LOG.E ("Mainactivity","Onsubscribe"); }@Override Public void OnNext(Bitmap Bitmap) {LOG.E ("Mainactivity","OnNext"); }@Override Public void OnError(Throwable e) {LOG.E ("Mainactivity","OnError"+ e.tostring ()); }@Override Public void OnComplete() {LOG.E ("Mainactivity","OnComplete"); } }); Break; } }
Well, that's OK, and then we run and what's going to happen to the log Nikki
com.liuguilin.schedulerssample E/MainActivity: onSubscribecom.liuguilin.schedulerssample E/MainActivity: android.os.NetworkOnMainThreadException
He suggested that we could not do network operation in the main thread, well, we can conclude that subscribe is in the main thread, and now our schedulers will come in handy, we just need
.subscribeOn(Schedulers.io())
Let it operate in I/O, and these meanings, also mentioned above, that we are now running, can see the following print
com.liuguilin.schedulerssample E/MainActivity: onSubscribecom.liuguilin.schedulerssample E/MainActivity: onNextcom.liuguilin.schedulerssample E/MainActivity: onComplete
Okay, let's go. OnNext instructions we loaded was successful, because I was loading the picture, all I now come to load this picture, this is very simple, we just need to set it up just fine
@OverridepublicvoidonNext(Bitmap bitmap) { Log.e("MainActivity""onNext"); iv_img.setImageBitmap(bitmap);}
But when you run, you'll find that there's another mistake.
thethatits views.
This error you must be unfamiliar, this is in the sub-thread update UI error, in fact, here is also very good understanding, is that we are in the network operation is in the sub-thread, yes, all of us to set the picture will have such a mistake, in fact, the solution is to switch our customers into our main UI thread is good, Add a row
.observeOn(AndroidSchedulers.mainThread())
Okay, now let's run a little bit and look at the effect.
OK, all the code is that little bit, just a little bit of knowledge, but be sure to understand his workflow
@Override Public void OnClick(FinalView v) {Switch(V.getid ()) { CaseR.id.load_img:observable.create (NewObservableonsubscribe<bitmap> () {//main thread execution @Override Public void Subscribe(FinalObservableemitter<bitmap> E1)throwsException {//Perform network operation resolution pictureOkhttpclient client =NewOkhttpclient (); Request Request =NewRequest.builder (). URL (img_url). build ();//Async methodClient.newcall (Request). Enqueue (NewCallback () {@Override Public void onfailure(Call call, IOException e) {E1.onerror (e); }@Override Public void Onresponse(Call call, Response Response)throwsIOException {if(Response.issuccessful ()) {byte[] bytes = response.body (). bytes (); Bitmap Bitmap = Bitmapfactory.decodebytearray (Bytes,0, bytes.length); E1.onnext (bitmap); E1.oncomplete (); } } }); }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()) . Subscribe (NewObserver<bitmap> () {@Override Public void Onsubscribe(Disposable D) {LOG.E ("Mainactivity","Onsubscribe"); }@Override Public void OnNext(Bitmap Bitmap) {LOG.E ("Mainactivity","OnNext"); Iv_img.setimagebitmap (bitmap); }@Override Public void OnError(Throwable e) {LOG.E ("Mainactivity","OnError"+ e.tostring ()); }@Override Public void OnComplete() {LOG.E ("Mainactivity","OnComplete"); } }); Break; } }
When the thread operation, oneself as long as clear use which one schedulers is good, this example is relatively simple, but if your logic is very complex, and need to switch back and forth, the advantage of this rxjava comes out, we later slowly in depth!
Sample Download: http://download.csdn.net/detail/qq_26787115/9792294
RX Series Five | Schedulers Line Program Control system