RxSwift:数据库事件和不同调度的问题

问题描述 投票:0回答:1

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

这会导致代码多次处理同一实体,因为在值再次更新到数据库之前会触发事件。

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
}

}
ios realm rx-swift
1个回答
1
投票

这篇文章将为您提供帮助:RxSwift 的 FlatMap 的许多方面

总结就是,flatMap 有几个不同的版本。您使用的是合并版本,这显然不是您想要的。考虑使用

flatMapFirst
flatMapLatest
concatMap
来代替。您使用哪一个将取决于您追求哪种行为。

© www.soinside.com 2019 - 2024. All rights reserved.