Reputation: 2663
The need is a little strange. I want to convert a list to a new list. For example, convert List<Android>
to List<AndroidWrapper>
.
class AndroidWrapper
has two fields.
public class AndroidWrapper {
private Android mAndroid;
private String mAvatarUrl;
public AndroidWrapper(Android android, String avatarUrl) {
this.mAndroid = android;
this.mAvatarUrl = avatarUrl;
}
}
The field mAvatar
is related to mAndroid
field and it can be fetched from remote server.
Observable<String> fetchAvatarUrl(Android android)
So how can do this using RxJava2
?
Upvotes: 2
Views: 826
Reputation: 70017
Turn your list into an Observable
, flatMap
the dependent call onto it, create the final object type and collect it into a list:
Observable.fromIterable(androids)
.flatMap(android ->
fetchAvatarUrl(android)
// go into a backgroud thread
.subscribeOn(Schedulers.io())
// combine the result with the original value
.map(url -> new AndroidWrapper(android, url))
// limit the maximum concurrent fetch calls
, 1
)
.toList();
Upvotes: 2