RXSwift + Moya +错误处理+刷新按钮

时间:2018-10-12 01:08:33

标签: swift rx-swift moya

我试图建立一个在按下按钮后刷新用户数据的表视图。 RXSwift用于整个事件链。 Moya用于路由。

我正在尝试使用Moya提供的标准错误处理,即:

provider.rx.request(.userProfile("ashfurrow")).subscribe { event in
    switch event {
    case let .success(response):
        image = UIImage(data: response.data)
    case let .error(error):
        print(error)
    }
}

我能够做到这一点的唯一方法是使用内部订阅方法。请参见下面的代码。谁能想到不需要内部订阅的方法?看起来有点笨拙。

class ViewController: UIViewController {
    @IBOutlet weak var refreshBtn: UIButton!
    @IBOutlet weak var tableView: UITableView!

    let provider = MoyaProvider<MyAPI>()

    let disposeBag = DisposeBag()

    var latestUsers = Variable<[User]>([])

    override func viewDidLoad() {
        super.viewDidLoad()

        setupObservableBtnRefreshWithDataFetch()
        bindDataToTableView()
    }

    func setupObservableBtnRefreshWithDataFetch() {
        let refreshStream = refreshBtn.rx.tap.startWith(())

        let responseStream = refreshStream.flatMapLatest { _ -> SharedSequence<DriverSharingStrategy, [User]> in
            let request = self.provider.rx.request(.showUsers)

            // Inner Subscribe here, to be able to use the standard Moya subscribe methods for error handling
            request.subscribe { event in
                switch event {
                case .success(let user):
                    print("Success")
                case .error(let error):
                    print("Error occurred: \(error.localizedDescription)")
                }
            }

            return request
                .filterSuccessfulStatusCodes()
                .map([User].self)
                .asDriver(onErrorJustReturn: [])
        }

        let nilOnRefreshTapStream: Observable<[User]> = refreshBtn.rx.tap.map { _ in return [] }
        let tableDisplayStream = Observable.of(responseStream, nilOnRefreshTapStream)
            .merge()
            .startWith([])

        tableDisplayStream
            .subscribe { event in
                switch event {
                case .next(let users):
                    print("Users are:")
                    print(users)
                    self.latestUsers.value = users
                    break
                case .completed:
                    break
                case .error(let error):
                    print("Error occurred: \(error.localizedDescription)")
                    break
                }
            }
            .disposed(by: self.disposeBag)
    }

    func bindDataToTableView() {
        latestUsers.asObservable()
            .bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { (_, model: User, cell: UITableViewCell) in
                cell.textLabel?.text = model.login
            }
            .disposed(by: disposeBag)
    }
}      

class User: Decodable {
    var name: String?
    var mobile: Int?
    var userRequestedTime: String?
    var login: String?

    init(name: String, mobile: Int, login: String = "") {
        self.name = name
        self.mobile = mobile
        self.login = login
    }
}

1 个答案:

答案 0 :(得分:0)

我研究了Moya,并了解它是网络操作的包装器。

尚不清楚内部订阅的用途是什么-根据我的理解,它会触发相同但独立的网络请求,这不应影响其他请求的订阅。 似乎refreshButton轻击在tableDisplayStream中发出两个元素(来自responseStream(来自refreshStream)和nilOnRefreshTapStream)。

请注意,不建议使用Variable。就个人而言,我也更喜欢.debug().subscribe()手动在订阅闭包中打印事件。

基于此,我将改为编写以下代码。我还没有测试。希望对您有帮助!

class ViewController: UIViewController {
    // ...

    private let provider = MoyaProvider<MyAPI>()
    private let disposeBag = DisposeBag()

    /// Variable<T> is deprecated; use BehaviorRelay instead
    private let users = BehaviorRelay<[User]>(value: [])

    private func setupObservableBtnRefreshWithDataFetch() {
        refreshBtn.rx.tap
           .startWith(()) // trigger initial load
           .flatMapLatest { _ in 
               self.provider.rx.request(.showUsers)
                   .debug("moya request")
                   .filterSuccessfulStatusCodes()
                   .map([User].self)
                   .asDriver(onErrorJustReturn: []) // don't let the error escape
           } 
           .drive(users)
           .disposed(by: disposeBag)
    }

    private func bindDataToTableView() {
        users
            .asDriver()
            .debug("driving table view ")
            .drive(tableView.rx.items /* ... */)
            .disposed(by: disposeBag)
    }
}