New to rxSwift and trying to learn a somewhat simple function. When .timeout
is used on an observable sequence it will return an error message Unhandled error happened: Sequence timeout.
if one of the observables in the sequence didn't emit an event.
This is my attempt at handling an observable no longer receiving events, if there is a better way to achieve this please suggest it!
How can I fire off an alert if the .timeout
operator returns an error message.
.timeout
information:
Summary
Applies a timeout policy for each element in the observable sequence. If the next element isn’t received within the specified timeout duration starting from its predecessor, a TimeoutError is propagated to the observer. Declaration
dueTime
Maximum duration between values before a timeout occurs.
scheduler
Scheduler to run the timeout timer on.
Returns
An observable sequence with a RxError.timeout in case of a timeout.
Code :
Observable.combineLatest(currentUser, opponent, game)
.timeout(3, scheduler: MainScheduler.instance)
.subscribe(onNext: { (arg) -> Void in
let (currentUser, opponent, game) = arg
if game.isPlayersTurn(currentUser) {
self._turnState.onNext(.yourTurn)
} else if game.isPlayersTurn(opponent) {
self._turnState.onNext(.theirTurn)
} else if game.isTie() {
self._turnState.onNext(.tie)
} else if game.gameData.winner == currentUser.userId {
self._turnState.onNext(.win(opponentWon: false))
} else if game.gameData.winner == opponent.userId {
self._turnState.onNext(.win(opponentWon: true))
}
})
.disposed(by: disposeBag)
}
Doing some documentation research I found rxSwift's onError: handler.
This will fire off upon receiving an errorEvent, from there I can run a function
onError: {err in errorAlert(); print("error \(err)")}
That solves the problem.