Skip to content

Instantly share code, notes, and snippets.

@torpedo87
Created January 21, 2019 06:51
Show Gist options
  • Save torpedo87/c4643aa1f7fe8561669295d9e6c35d6a to your computer and use it in GitHub Desktop.
Save torpedo87/c4643aa1f7fe8561669295d9e6c35d6a to your computer and use it in GitHub Desktop.
error handling

error handling

  • 에러발생시 시퀀스가 종료하지 않도록 처리해야한다

throw

  • When you want to error out inside a flatMap operator, you should use throw as in regular Swift code

catch

  • default value로 처리하기
  • 일반적으로 이전에 캐싱해놓은 값으로 처리

catchError(_:)

  • Continues an observable sequence that is terminated by an error with the next observable sequence.

catchErrorJustReturn(_:)

  • Continues an observable sequence that is terminated by an error with a single element.

retry

  • 에러나면 다시 시도하기

retry()

  • Repeats the source observable sequence until it successfully terminates.
  • 될때까지 무한 시도

retry(_ maxAttemptCount:)

  • Repeats the source observable sequence the specified number of times in case of an error or until it successfully terminates.
  • 될때까지 제한된 시도

retryWhen(_:)

  • Repeats the source observable sequence on error when the notifier emits a next value. If the source observable errors and the notifier completes, it will complete the source sequence.
  • trigger 역할을 하는 handler 를 사용해서 에러를 정교하게 처리할 수 있다
let retryHandler: (Observable<Error>) -> Observable<Int> = { e in
      return e.enumerated().flatMap{ (attempt, error) -> Observable<Int> in
        if attempt >= maxAttempts - 1 {
          return Observable.error(error)
        }
        print("== retry after \(attempt + 1) seconds ==")
        return Observable<Int>.timer(Double(attempt + 1), scheduler: MainScheduler.instance).take(1)
      }
    }

do(onError: )

  • 에러났을 때 side effect 실행하기
  • 에러시 화면에 메시지 표시하는 경

materialize

  • 시퀀스의 모든 element를 next 이벤트로 감싸서 내보내주므로 에러가 발생해도 시퀀스가 종료되지 않게 한다
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment