0
votes

I try to use retryOnBecomesReachable method from the RX example files in my networking layer

extension ObservableConvertibleType {
    func retryOnBecomesReachable(_ valueOnFailure:E, reachabilityService: ReachabilityService?) -> Observable<E> {
        return self.asObservable()
            .catchError { (e) -> Observable<E> in
                return reachabilityService.reachability
                    .skip(1)
                    .filter { $0.reachable }
                    .flatMap({ _ -> Observable<E> in
                        return Observable.error(e)
                    })
                    .startWith(valueOnFailure)
            }
            .retry()
    }
}

// My layer

request
    .flatMapLatest{ request  in
        provider.rx.request(request)
            .map{ User.self }
            .map{ RequestState.loaded($0) }
            .retryOnBecomesReachable(.error(.notConnectedToInternet), reachabilityService: reachabilityService)
            .catchError({ .just(.error($0)) })
            .startWith(.startLoading)
}

Without this method, all works awesome. All error catching and returning .just(.error($0)) sequence.

With this method, the retry feature works awesome. But when something happens (mapping, decoding or other error) I get .notConnectedToInternet. I think the reason in .startWith(valueOnFailure) method. I tried to move, remove, change position but nothing helps. I'm stuck.

What should I do to use retry feature and catch errors correct?

1

1 Answers

0
votes

I think that basically changing .startWith(valueOnFailure) to startWith(e) might work for you. Another option is to check if the error is a reachability error to begin with inside the catch block.

e.g.

.catchError { e in 
   guard e == SomeError.notConnectedToInternet else {
       return .error(e)
   }

   ... rest of your code