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:
- In many app types will have image upload function, such as the evaluation of goods,
- The client allows the user to take a photo upload (possibly multiple),
- Upload pictures to the cloud (now many small and medium-sized companies are using the cloud as a picture server),
- Then get the URL of the picture, and then send the picture information (image URL, image size) to the picture.
Main logic:
- First upload all the pictures to the cloud (like 3 pictures)
- Get picture URL path, picture size, etc.
- 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