You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

2 years ago
  1. //
  2. // ignoreErrors.swift
  3. // RxSwiftExt
  4. //
  5. // Created by Florent Pillet on 18/05/16.
  6. // Copyright © 2016 RxSwift Community. All rights reserved.
  7. //
  8. import RxSwift
  9. extension ObservableType {
  10. /**
  11. Unconditionally ignore all errors produced by the source observable, effectively producing a sequence
  12. that never fails (any error will simply have no effect on the sequence).
  13. - returns: An observable sequence that never fails
  14. - seealso: `retry` operator
  15. */
  16. public func ignoreErrors() -> Observable<Element> {
  17. return retry()
  18. }
  19. /**
  20. Conditionally ignore errors produced by the source observable
  21. - parameter predicate a predicate called when an error occurs and returns `true` to ignore the error (continuing), `false` to terminate the sequence with the given error.
  22. - returns: An observable sequence that errors only when `predicate` returns `false`
  23. */
  24. public func ignoreErrors(_ predicate : @escaping (Error) -> Bool) -> Observable<Element> {
  25. return retry(when: {
  26. return $0.flatMap { error -> Observable<Bool> in
  27. return predicate(error) ? Observable.just(true) : Observable<Bool>.error(error)
  28. }
  29. })
  30. }
  31. }