AndrewSB AndrewSB - 20 days ago 8
Swift Question

RxSwift flatMap latest one time without disposing

Had a quick question: I have an

Variable<[Session]>
, where
Session
:

class Session {
...
var rx_serverRequestable: Driver<SessionRequestable>
...
}


which emits a .next event every time the session has all the information it needs to be passed on to the backend and I want to be able to
flatMapLatest
on the array of sessions, and do something like:

let sessions: Variable<[Session]>
sessions
.flatMapLatest { sessions in sessions.map { $0. rx_serverRequestable } }
.flatMap { $0.requestFromServer() }


but I only want to request each session once. There are two ways I see that failing with my current implementation:
1. flatMapLatest gets a new array of sessions, potentially disposing a request from server thats still in progress
2. rx_serverRequestable gets called each time the session has all the information required to be loaded from server, so it will get called multiple times, each time the session loads in any new information. I only want the session to be requested the first time, should I be using something like
.multicast
or
.replay(1)
?

any pointers on solving the two issues, or switching up my approach?

Answer

This can be solved using the zip operator http://reactivex.io/documentation/operators/zip.html

Comments