如何调用将附加内容加载到将其转换为另一个可观察对象的对象的可观察对象?

How to call observable that loads additional contents to a object transforming it in another observable?

我有这个 MyCollectionInteractor 从 firebase 数据库加载所有 CollectionItemVO

public interface MyCollectionInteractor extends BaseInteractor{
    Single<List<CollectionItemVO>> load ();
}

CollectionItemVO 是:

public class CollectionItemVO {
    String beerId;
    long timestamp;
    int quantity;

    public CollectionItemVO() {
    }


    public CollectionItemVO(String beerId, long timestamp, int quantity) {
        this.beerId = beerId;
        this.timestamp = timestamp;
        this.quantity = quantity;
    }
}

所以我有这个 CollectionItem:

public class CollectionItem {

    private final CollectionItemVO itemVOList;
    private final Beer beer;

    public CollectionItem(Beer beer, CollectionItemVO itemVOList) {
        this.beer = beer;
        this.itemVOList = itemVOList;
    }

}

那有一个完整的 Beer 对象。为了加载该对象,我使用了另一个交互器:

public interface LoadBeerInteractor extends BaseInteractor {
    Flowable<Beer> load(String beerId);
}

我想将此 CollectionInteractor.load 调用转换为发出 CollectionItemObservable,并且我想使用 LoadBeerInteractor.load(beerId) 来交付带有完整啤酒对象的 CollectionItem。

就我所学的内容而言,我相信使用平面图可以做到这一点,但我还没有达到预期的效果。

我认为你需要做这样的事情:

MyCollectionInteractor collections = ...
LoadBeerInteractor beers = ...

Flowable<CollectionItem> items = collections.load()
    .toFlowable()
    .flatMapIterable(it -> it) // unpack from Flow<List<T>> to Flow<T>
    .flatMap(it ->
        beers
            .load(it.beerId)
            .map(beer -> new CollectionItem(beer, it))
    )

这也可能有效:

Flowable<CollectionItem> items = collections.load()
    .toFlowable()
    .flatMap(list ->
        Flowable
            .from(list)
            .flatMap(it -> 
                beers
                    .load(it.beerId)
                    .map(beer -> new CollectionItem(beer, it))
            )
    )