简体   繁体   English

处理UITableView绑定中的连接错误(Moya,RxSwift,RxCocoa)

[英]Handle Connection Error in UITableView Binding (Moya, RxSwift, RxCocoa)

I'm using RxCoCoa and RxSwift for UITableView Biding. 我将RxCoCoa和RxSwift用于UITableView出价。 the problem is when Connection lost or other connection errors except for Server Errors(I handled them) my app crash because of binding error that mentioned below. 问题是由于以下提到的绑定错误导致连接丢失或服务器错误(我已处理)以外的其他连接错误导致我的应用崩溃。 my question is how to handle Connection Errors? 我的问题是如何处理连接错误?

fileprivate func getNextState() {
        showFullPageState(State.LOADING)
        viewModel.getProductListByID(orderGroup: OrderGroup.SERVICES.rawValue)
                .do(onError: {
                    showStatusError(error: $0)
                    self.showFullPageState(State.CONTENT)
                })
                .filter {
                    $0.products != nil
                }
                .map {
                    $0.products!
                }
                .bind(to: (self.tableView?.rx.items(cellIdentifier: cellIdentifier, cellType: ProductCell.self))!) {
                    (row, element, cell) in
                    self.showFullPageState(State.CONTENT)
                    cell.product = element
                }

                .disposed(by: bag)
        self.tableView?.rx.setDelegate(self).disposed(by: bag)
    }

and this is my ViewModel : 这是我的ViewModel

func getProductListByID(orderGroup: String, page: String = "1", limit: String = "1000") -> Observable<ProductRes> {
        return orderRegApiClient.getProductsById(query: getProductQueryDic(stateKey: getNextStateID(product: nextProduct)
                , type: orderGroup, page: page, limit: limit)).map {
            try JSONDecoder().decode(ProductRes.self, from: $0.data)
        }.asObservable()
    }

and I use Moya for my Network layer like This: 我将Moya用于网络层,如下所示:

func getProductsById(query: [String: String]) -> Single<Response> {
        return provider.rx.request(.getProductsById(query))
                .filterSuccessfulStatusCodes()
    }

在此处输入图片说明

You aren't handling errors anywhere. 您不会在任何地方处理错误。 I mean you are acknowledging the error in the do operator but that doesn't actually handle it, that just allows it to pass through to the table view, which can't handle an error. 我的意思是您在do运算符中确认了错误,但实际上并不能处理该错误,仅允许它传递到无法处理错误的表视图。

Look up the catchError series of operators for a solution. 查找catchError系列运算符以获取解决方案。 Probably .catchErrorJustReturn([]) will be all you need. .catchErrorJustReturn([])可能就是您所需要的。


In a comment, you said: 在评论中,您说:

... I don't want to return empty Array to my table. ...我不想将空数组返回到我的表。 I want to show the error to customer and customer can retry service 我想向客户显示错误,客户可以重试服务

In that case, you should use .catchError only for the success chain and setup a separate chain for the error as done below. 在这种情况下,您应该仅将.catchError用于成功链,并为错误建立单独的链,如下所示。

fileprivate func getNextState() {
    showFullPageState(State.LOADING)
    let products = viewModel.getProductListByID(orderGroup: OrderGroup.SERVICES.rawValue)
        .share()

    products
        .catchError { _ in Observable.never() }
        .filter { $0.products != nil }
        .map { $0.products! }
        .bind(to: tableView!.rx.items(cellIdentifier: cellIdentifier, cellType: ProductCell.self)) {
            (row, element, cell) in
            self.showFullPageState(State.CONTENT)
            cell.product = element
        }
        .disposed(by: bag)

    products
        .subscribe(onError: { error in
            showStatusError(error: error)
            self.showFullPageState(State.CONTENT)
        })
        .disposed(by: bag)

    self.tableView?.rx.setDelegate(self).disposed(by: bag)
}

The way you have the code setup, the only way for the user to retry the service is to call the function again. 您具有代码设置的方式,用户重试该服务的唯一方法是再次调用该函数。 If you want to let the user retry in a more declarative manor, you would need to tie the chain to an observable that the user can trigger. 如果要让用户以更具声明性的方式重试,则需要将链与用户可以触发的可观察对象绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM