0

我有这个代码:

var kitchenList: MutableList<Dish> = ArrayList()
var intervalObser = interval(1, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())

        
        intervalObser.subscribe({
            fromIterable(orderList)
                .filter { it.status.equals("to create")
                }
                .subscribe({
                    kitchenList.add(it)
                    Log.d("add", "success")
                },{Log.d("add", "error")})
        },{})

他在哪里找到要完成的饭菜并每秒将它们添加到厨房的列表中,但是如何使用 rxJava 检查有关特定 id 的数据是否已经在 kitchenList 上?以及如何停止添加 10 个菜肴并在删除一个或多个后重新添加。

除了上面使用的间隔之外,还有其他方法负责重复吗?

4

1 回答 1

0

你可以做这样的事情(虽然它对于产生所需的行为有点复杂):

val orderList: MutableList<Dish> = ArrayList()
val kitchenList: MutableList<Dish> = ArrayList()

fun setCreated(id: Int) : Unit {
    Observable.fromIterable(orderList)
    .filter { it.id == id }
    .doOnNext { it.status = "created" }
    .subscribe()

    Observable.fromIterable(kitchenList)
    .filter { it.id != id }
    .toList()
    .doOnSuccess { 
        kitchenList.clear()
        kitchenList.addAll(it)
    }
    .subscribe()
 
    updateKitchen()
}
fun updateKitchen() : Unit {
    Observable.fromIterable(orderList)
    .filter { it.status == "to create" }
    .flatMapMaybe { outer ->
         Observable.fromIterable(kitchenList)
         .any { it.id == outer.id }
         .filter { !it }
         .map { outer }
    }
    .takeWhile { kitchenList.size < 10 }
    .doOnNext { kitchenList.add(it) }
    .subscribe()
}
于 2021-06-10T10:52:17.150 回答