处理服务器响应rxswift

时间:2019-04-15 12:08:23

标签: swift rx-swift

我有一个连接到API的应用程序,该API的状态可能是成功还是出错,直到最近,该应用程序都可以正常运行,直到出现问题,即暂停服务并在检查期间改为通过api调用进入错误状态消息,状态为〜fail〜,因此,该应用程序仍保留在屏幕上,而未显示诸如“发生错误,请重试”之类的消息。如何解决对〜fail〜消息的支持?

网络

func refreshActiveTrip() -> Observable<MaxResponse<Trip>> {
        return ApiClient.session.rx.request(urlRequest: MaxApiRouter.getActiveTrip(userId: getUser()!.id!))
            .convert(to: MaxResponse<Trip>.self)
            .catchError({ error in
                // Server returns 404 when the user has no active trip
                if let error = error as? NetworkingError, let code = error.httpResponse?.statusCode,
                    code == HTTPStatus.notFound.rawValue {
                    return Observable<MaxResponse<Trip>>.just(MaxResponse<Trip>())
                } else {
                    return Observable.error(error)
                }})
            .do(onNext: { response in
                if let trip = response.data {
                    self.storage.saveTrip(trip)
                } else {
                    self.storage.deleteTrip()
                }
            })

    }

称为

func refreshActiveTrip() {
        source.refreshActiveTrip()
            .retry(.delayed(maxCount: 100, time: 2.5), shouldRetry: networkRetryPredicate)
            .asObservable()
            .take(1)
            .subscribe(onNext: {[weak self]  response in
                print("Next")
            }, onError: {[weak self] (error) in
                print("Error")
            }, onCompleted: {
                print("Completed")
            }, onDisposed: {
                print("Disposed")
            }).disposed(by: disposeBag)
}

0 个答案:

没有答案