1
votes

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)
  }
2
Are you just looking for an alert to display when the timeout comes and if so hold you want the user to have to tap the alert to continue or just display a notification? - Talon Brown
@TalonBrown looking to display an alert when the timeout comes - Joshua

2 Answers

0
votes

To make a UIAlert

First, make an alertController.

let avc = UIAlertController(title: "Your Title Here", message: "The message you wish to display", preferredStyle: .alert)

Next, you need to make actions; these have the options of executing code in a completion handler after the actions button has been pressed

    let yourAction = UIAlertAction(title: "Save", style: .default) { (action) in
        //Your code that wishes to be executed after the butt has been pressed
    }

Then, add the action into the controller

avc.addAction(yourAction)

then all you need to do is present the alert

present(avc, animated: true, completion: nil)
0
votes

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.