iosrealmrx-swift

RxSwift: Problem with DB events and different Schedular


I am using the realm as backend. And using RxSwift I have an observable to DB table for some column (it tracks pending state). After I get the event I switch to another Serial schedular to set the value of the column to Progress state in flatMap block (which returns completable). Now my problem is in case of too many events in DB my observable triggers multiple times and doesn't wait for my flatMap block to finish completely.

This results that the code process the same entity multiple times because before values are updated to DB again event gets triggered.

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
}

}

Solution

  • This article will help you: RxSwift's Many Faces of FlatMap.

    In summary, there are several different versions of flatMap. The one you are using is a merging version which apparently isn't want you want. Consider using either flatMapFirst, flatMapLatest, or concatMap instead. Which of these you use will depend on which behavior you are after.