RxSwift:数据库事件和不同的计划时间问题

问题描述

我正在使用领域作为后端。使用RxSwift,我可以在DB表中观察到某些列(它跟踪未决状态)。得到事件后,我切换到另一个Serial计划表,以将列的值设置为flatMap块中的Progress状态(返回可完成)。现在我的问题是,如果数据库中有太多事件,我的可观察触发器就会多次触发,而不必等待我的flatMap块完全完成。

这导致代码多次处理同一个实体,因为在将值再次更新到DB之前再次触发了事件。

Realm func

func downloadIconForUsers() -> Observable<[Person]> {

// here I do fetch on the main thread and return for a filter for PENDING STATE
}



realmDB.downloadIconForUsers()
.observeOn(differentSchedular)
.filter { !$0.isEmpty}
.flatMap {
   //map over all entity and change state to progress
   let allProgress = 0.map { //map over all entity and change state to progress }
    userDB.updatePerson($0).do {
    //onCompleted
    Download all pending state icons
}

}

解决方法

本文将为您提供帮助:RxSwift's Many Faces of FlatMap

TL; DR是flatMap有几种不同的版本。您正在使用的是一个合并版本,显然是您不需要。考虑改用flatMapFirstflatMapLatestconcatMap。您将使用哪种方式取决于您采取的行为。