RxJava zip operator in Android real-life scenarios

Source: Internet
Author: User

Overview

Returns an Observable, emits the results of a specified combiner function applied to combinations of the items emitted ,
In sequence, by and other observables.

Flow chart:

In short, the zip operator merges multiple data streams,
Then send (Emit) the final merged data.

Requirements Description:

    1. In many app types will have image upload function, such as the evaluation of goods,
    2. The client allows the user to take a photo upload (possibly multiple),
    3. Upload pictures to the cloud (now many small and medium-sized companies are using the cloud as a picture server),
    4. Then get the URL of the picture, and then send the picture information (image URL, image size) to the picture.

Main logic:

    1. First upload all the pictures to the cloud (like 3 pictures)
    2. Get picture URL path, picture size, etc.
    3. Finally, all data is submitted to the server
Images that need to be uploaded picture[] ps = xxx;Observable. zip(Observable. from(PS), getupyunaddress (PS. Length),//Get uploaded URL new func2<picture, upyunaddress, picture> () {@Override Public picturePager(Picture picture, upyunaddress upyunaddress) {//If the picture has been uploaded, it should not be uploaded if (textutils. IsEmpty(Picture. GetSource())) {try {//Use another tool class provided by the cloud, upload image S Tring Path = Upyunutil. Uploadimage(Upyunaddress, picture. Getlocalurl());Gets the final URL of String FinalURL = upyunaddress. Getprefix() + Path;Picture. SetSource(FinalURL);} catch (Exception e) {E. Printstacktrace();}} return picture;}                }). Subscribeon(schedulers. IO()). Observeon(androidschedulers. Mainthread())////upload to get image size after success. FlatMap(New Func1<picture, observable<picture>> () {@Override public Observable <Picture>Pager(Picture picture) {if (textutils. IsEmpty(Picture. GetHeight()) || Textutils. IsEmpty(Picture. GetWidth())) {Bitmapfactory. OptionsOptions;if (! Textutils. IsEmpty(Picture. Getlocalurl()) {options = Imageutil. Getbitmapoptions(Picture. Getlocalurl());Picture. Setlocalurl(NULL);} else {options = Imageutil. Getbitmapoptions(Picture. GetSource());} picture. SetWidth(String. ValueOf(Options. Outwidth));Picture. SetHeight(String. ValueOf(Options. Outheight));} return Observable. Just(picture);}                });Finally, the final data is processed.

Article turned from: http://blog.csdn.net/johnny901114/article/details/51614927

Thank the original author for sharing!

RxJava zip operator in Android real-life scenarios

Related Article

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.