应该使用哪个运算符将许多Flowable<CurrencyStamp>
转换为Flowable<List<CurrencyStamp>>
?
转换代码:
public static Single<List<CurrencyStamp>> getStampByDay(String symbol, Date date, String... convertsSymbols){
long count = 0;
Single<List<CurrencyStamp>> result = null;
while (count < secByDay){
Flowable<CurrencyStamp> item = CoinApi.getCompareApi().getCurrencyHistory(symbol, date.getTime() - count,
convertsSymbols).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
count += secByFiveMin;
}
return result;
}
在“ while”构造中,我需要将每个“ item”添加到“ base”并在结果中创建Single<List<Currency>>
(例如)
答案 0 :(得分:1)
要从Flowable转换为Flowable>,您需要使用Flowable#toList,它将返回Single>
Single<List<Integer>> listSingle = Flowable.just(1, 2, 3).toList();
请记住,给定的Single将仅返回一个值或错误。给定Flowable完成后,将发出收集的列表。 Flowable中的OnError将与onError一起传播。
警告:当Flowable为无穷大时,将导致内存泄漏,因为由于给定Flowable没有“ onComplete”,因此列表永远不会被发出。
答案 1 :(得分:0)
我写这段代码:
public static Single<Currencies> getCurrencyFlowable(){
Single<Currencies> usd = CoinApi.getMarketCupApi().getCurrencies();
Single<Currencies> rub = CoinApi.getMarketCupApi().getCurrencies("RUB");
Single<Currencies> eur = CoinApi.getMarketCupApi().getCurrencies("EUR");
Single<Currencies> btc = CoinApi.getMarketCupApi().getCurrencies("BTC");
List<Single<Currencies>> singles = new ArrayList<>();
singles.add(usd);
singles.add(rub);
singles.add(eur);
singles.add(btc);
return Single.zip(singles, objects -> {
Currencies[] currencies = new Currencies[objects.length];
for (int i = 0; i < objects.length; i++) {
currencies[i] = (Currencies)objects[i];
}
return ramming(currencies);
}).subscribeOn(Schedulers.io() )
.observeOn(AndroidSchedulers.mainThread());
}
为我工作。