Why not Rxjava?

Source: Internet
Author: User

Rxjava and other products. Thought is good, but most people use it as a piece of crap!

Take Rx's classic example:

Suppose there is a requirement: there is a custom view on the interface that imageCollectorView is used to display multiple images and to use addImage(Bitmap) the method to arbitrarily add the displayed picture. Now it is necessary for the program to load the PNG images in each directory in a given directory array File[] folders and display them in imageCollectorView . It is important to note that because the process of reading a picture is time-consuming, it needs to be performed in the background, and the display of the picture must be performed on the UI thread. There are a number of common implementations, which I post here:

NewThread () {@Override Public voidrun () {Super. Run ();  for(File folder:folders) {file[] files=Folder.listfiles ();  for(File file:files) {if(File.getname (). EndsWith (". png")) {                    FinalBitmap Bitmap =getbitmapfromfile (file); Getactivity (). Runonuithread (NewRunnable () {@Override Public voidrun () {imagecollectorview.addimage (bitmap);                }                    }); }}}}}.start ();

Hard to see a bit cumbersome, should be able to optimize under, don't hurry first see Rx how to achieve.

Rxjava:

observable.from (folders). FLATMAP (NewFunc1<file, observable<file>>() {@Override PublicObservable<file>Call (file file) {returnObservable.from (File.listfiles ()); }}). filter (NewFunc1<file, boolean>() {@Override PublicBoolean Call (file file) {returnFile.getname (). EndsWith (". png"); }}). map (NewFunc1<file, bitmap>() {@Override PublicBitmap Call (file file) {returngetbitmapfromfile (file); }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe (NewAction1<bitmap>() {@Override Public voidCall (Bitmap Bitmap) {imagecollectorview.addimage (BITMAP); }    });

A look at Mdzz (Madeleine's staff), a closer look at Mdzzz (Madeleine's staff)----> code is this not brain-retarded? Do you know what is called decoupling?

My implementation:

 //----------start loading all pictures      Public voidaddallimg () {x.task (). Run (NewRunnable () {//I use XUTILS3 asynchronous task@Override Public voidrun () { for(File folder:folders) {//Traverse Folderfile[] Files =Folder.listfiles ();  for(File file:files) {//traversing Filesaddallimg_1 (file);//call subsequent methods to process                     }                 }             }         }); }     Private voidAddallimg_1 (file file) {//processing, updating UI, etc.         if(File.getname (). EndsWith (". png")) {             FinalBitmap Bitmap =getbitmapfromfile (file); Getactivity (). Runonuithread (NewRunnable () {@Override Public voidrun () {imagecollectorview.addimage (bitmap);         }             }); }     }   
// ----------Load all pictures finished

RX Series shortcomings too many, any one is enough reason to use him, because there is a better way to achieve

1. Excessive use of the design pattern, logic is only one but the code was written by you and the chain is also intoxicating

What is chain, see my realization ... Support continued expansion 2 3 4 5 6 ... n

private void Addallimg_2 ()
private void Addallimg_3 ()

...

2. Good ideas do not understand the rational use, see how android-lesscallback is used

Performed by ID order

 Public classTest2 {@TaskFlow (thread=true, id = 0)     Public voidDoTask1 () {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + "Task 1");    WaitTime (); } @TaskFlow (Thread=false, id = 1)     Public voidDotask1finish () {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + "Task 1 execution End"); } @TaskFlow (Thread=true, id = 2)     Public voidDoTask2 () {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + "Task 2");    WaitTime (); } @TaskFlow (Thread=false, id = 3)     Public voidDotask2finish () {System.out.println (Thread.CurrentThread (). GetName ()+ ":" + "Task 2 execution End"); }     Public Static voidMain (string[] args) {Taskflowutils.inject (NewTest2 ()). run ();; }     Public Static voidWaitTime () {intx =NewRandom (). Nextint (1000) + 500; Try{System.out.println ("Wait" +x+ "milliseconds");        Thread.Sleep (x); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

In my opinion, it is similar to RX, which uses the idea of next, which is different:

Lesscallback implementation complex, call simple

RX Implementation Complex, call also special complex

3. The coupling degree is too large to lead to difficult debugging problems ... An eyeful of next subscribe observe

When you write a question, you may not find the maintenance time you understand.

4. Learning difficult, want to eat a beef skewer you let me learn to slaughter cattle first?

5. Use less interface if you can, try not to use (interface is a burrowing behavior, unless it is unavoidable, do not burrow)

For all your network requests, write a interface. I cannot understand that every page has its own function, and it is a distributed idea to implement its own function in its own pages (functions).

A random search is rx how good, support what what can extend what what what Balabala ... Really have their own understanding of how many?

A group of side dishes than

The Boulevard to Jane, if understood profoundly, uses it to be very simple, to make out so complex thing only to explain that you understand not very deep.

Why not Rxjava?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.