gpt4 book ai didi

android - 我的doOnComplete在.Map完成之前被调用

转载 作者:行者123 更新时间:2023-12-02 13:40:42 25 4
gpt4 key购买 nike

我对kotlin可观察变量有疑问,我已经通过Internet和stackoverflow搜索过,但是我认为我在概念上缺少了一些东西。我有dashboardRepository,它具有名为getCallsCountForWeek的方法,这基本上返回了过去7天的可流动列表,现在我需要遍历所有可流动对象,然后使用当天的用户调用次数来更新我的图表。这是我的代码

fun getCallsCountForWeek(calendar: Calendar) : List<Flowable<Float>> {

val result = ArrayList<Flowable<Float>>()

for(index in 0..6) {
calendar.add(Calendar.DAY_OF_MONTH, -index)
result.add(dashbordDao.getCallsCountForDay(customSharedPreferences.getUser()?.id!!, CustomDateTimeUtil.getStartOfDay(calendar), CustomDateTimeUtil.getEndOfDay(calendar)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()))
}

return result
}


Observable.fromArray(dashboardRepository
.getCallsCountForWeek(calendar). map {
items -> kotlin.run {
items.forEach {
it.subscribe({
Log.e("Result", " Count: " + it)
},{
Log.e("Error", "" + it)
})
}
}
}.doOnComplete {
//We will do this when it is completed
Log.e("Result", "Completed")
}.doFinally {
Log.e("Result", "Finally")
}.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe()
问题在于,在map通过所有flowable完成迭代之前,将调用doFinally和doOnComplete。我尝试将.zip用于flowables,但是显然无法使其正常工作。
根据堆栈溢出的其他帖子,订阅成功时将调用doOnComplete,但我希望在.map中完成所有操作后再进行。

最佳答案

您应该使用flatMapflatMapIterable而不是map,并且只有一个subscribe调用

 Observable.fromArray(dashboardRepository
.getCallsCountForWeek(calendar)
.flatMapIterable { it } // iterate over list
.flatMap { it } // use flowables from list
.doOnNext { /* do something with every item */ }
.doOnComplete {
//We will do this when it is completed
Log.e("Result", "Completed")
}
.doFinally {
Log.e("Result", "Finally")

}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.ignoreElements () // if you already handled everything in the doOnNext
.subscribe()

关于android - 我的doOnComplete在.Map完成之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63735094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com