java - Combining to a Map of parallelly downloaded images with RxJava Observables -


i'm trying parallel download of list of images, combining them map.

at first tried make observable this:

observable<map<integer, bitmap>> getimages(final list<activity> activities) {     return observable.create(new observable.onsubscribe<map<integer, bitmap>>() {         @override         public void call(subscriber<? super map<integer, bitmap>> subscriber) {             try {                 map<integer, bitmap> result = new hashmap<integer, bitmap>();                 (activity act : activities) {                     result.put(act.getid(), downloadimage(act.getimage()));                 }                 subscriber.onnext(result);                 subscriber.oncompleted();             } catch (exception e) {                 subscriber.onerror(e);             }         }     }); } 

this works, it's not want. because images being downloaded sequentially. , think for-loops not nice in rxjava. created these observables:

    observable<bitmap> getimage(final activity activity) {     return observable.create(new observable.onsubscribe<bitmap>() {         @override         public void call(subscriber<? super bitmap> subscriber) {             try {                 subscriber.onnext(downloadimage(activity.getimage()));                 subscriber.oncompleted();             } catch (exception e) {                 subscriber.onerror(e);             }         }     }); }  observable<map<integer, bitmap>> getimages(final list<activity> activities) {     return observable         .from(activities)         .flatmap(new func1<activity, observable<bitmap>>() {             @override             public observable<bitmap> call(activity activity) {                 return getimage(activity);             }         })         .tomap(new func1<bitmap, integer>() {             @override             public integer call(bitmap bitmap) {                 return 1; // how supposed activity.getid()?             }         }); } 

so made observable getting single image, trying combine them in second 1 using flatmap. works, there still 2 problems:

  1. when tomap(), how can retrieve right id use key map? want use activity object that.
  2. unfortunately, downloads still beging processed sequentially , not in parallel. how can fix this?

create wrapper class hold bitmap , activity. activitybitmap. replace getimage getactivitybitmap:

observable<activitybitmap> getactivitybitmap(final activity activity) {     return observable.create(new observable.onsubscribe<activitybitmap>() {         @override         public void call(subscriber<? super activitybitmap> subscriber) {             try {                 subscriber.onnext(new activitybitmap(activity, downloadimage(activity.getimage())));                 subscriber.oncompleted();             } catch (exception e) {                 subscriber.onerror(e);             }         }     }); } 

and call below. note asynchronous downloads use subscribeon in flatmap. build map<integer,bitmap> @ end use different overload of tomap allows specify key , value.

observable<map<integer, bitmap>> getimages(final list<activity> activities) {     return observable         .from(activities)         .flatmap(new func1<activity, observable<activitybitmap>>() {             @override             public observable<activitybitmap> call(activity activity) {                 return getactivitybitmap(activity).subscribeon(schedulers.io());             }         })         .tomap(new func1<activitybitmap, integer>() {             @override             public integer call(activitybitmap activitybitmap) {                 return activitybitmap.getactivity().getid();             }         },new func1<activitybitmap, bitmap>() {             @override             public integer call(activitybitmap activitybitmap) {                 return activitybitmap.getbitmap();             }         }); } 

Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -