RxSwift 从一个创建多个 Observable

Posted

技术标签:

【中文标题】RxSwift 从一个创建多个 Observable【英文标题】:RxSwift create multiple Observable from One 【发布时间】:2018-08-04 00:26:02 【问题描述】:

如何共享一个 API 调用?例如,我有以下模型类。这些属性将绑定到不同的 UI 组件。我在 ViewController 中实现这个。但是,我认为我不应该将模型暴露给视图层。相反,我应该给 ViewController 绑定两个 Observable 属性。一个用于 UITextField,另一个用于 UITableView。那么,我应该如何从一个 Observable 创建多个 Observable?

struct CustomerListJSON : Codable 
    var UserCount:Int?
    var TotalReward:Int?
    var CurMonthUserCount:Int?
    var CurMonthTotalReward:Int?
    var CurYearUserCount:Int?
    var CurYearTotalReward:Int?
    var UserList:[CustomerJSON]?


let getSummery = self.viewModel.getSummery()

getSummery.flatMap [weak self](summery) ->  Observable<[CustomerJSON]> in

//Label datesource
            self?.summeryLabel.text = " \(summery.UserCount ?? 0)\t \(summery.TotalReward ?? 0)\n \(summery.CurMonthUserCount ?? 0)\t \(summery.CurMonthTotalReward ?? 0)\n \(summery.CurYearUserCount ?? 0)\t\(summery.CurYearTotalReward ?? 0)"

            return Observable.of(summery.UserList ?? [])
//Table datasource
        .bind(to: self.tableView.rx.items(cellIdentifier: "customerCell", cellType: UITableViewCell.self)) (index, customer, cell) in
            cell.textLabel?.text = "\(customer.UserName ?? "")\t\t\(customer.CreateTime ?? "")"
        .disposed(by: rx.disposeBag)

【问题讨论】:

【参考方案1】:

我有这样一个不成熟的解决方案。因此,我可以将这两个 Observable 绑定到 ViewController 中的 UI 以隐藏 Model 结构。

但是,API 调用无法共享。这是另一个问题~

    fileprivate func apiCall() -> Observable<Response> 
                let userID = UserModel.sharedInstance.uID

                return self.dateParameterTrigger
                    .observeOn(self.queue)
                    .flatMap para -> Observable<Response> in
                        let startDateStr = para.0
                        let endDateStr = para.1
                        return self.provider.rx.request(CustomerTarget.GetInvitedUserList(userID, 0, 20, startDateStr, endDateStr))
                               .asObservable()
                               .share(replay: 1, scope: SubjectLifetimeScope.forever)
                    

            

            func getSummery() -> Observable<CustomerListJSON> 

                return apiCall()
                    .flatMap event -> Observable<CustomerListJSON> in
                        let catchReturn = Observable.of(CustomerListJSON())
                        var baseModel:CustomerModel!

                            do
                                let decoder = JSONDecoder()
                                baseModel = try decoder.decode(CustomerModel.self, from: event.data)
                            catch let error 
                                print( error )
                                return catchReturn
                            

                            guard let model = baseModel, let customerModel = model.returnValue else 
                                return catchReturn
                            

                            return Observable.just(customerModel)
                
            

            func getCustomerList() -> Observable<[CustomerJSON]> 

                return self.apiCall()
                    .flatMap event -> Observable<[CustomerJSON]> in
                        let catchReturn = Observable.of([CustomerJSON]())
                        var baseModel:CustomerModel!
                        do
                            let decoder = JSONDecoder()
                            baseModel = try decoder.decode(CustomerModel.self, from: event.data)
                        catch let error 
                            print( error )
                            return catchReturn
                        

                        guard let model = baseModel, let customerModel = model.returnValue, let customerList = customerModel.UserList else 
                            return catchReturn
                        

                        return Observable.just(customerList)
                
            

【讨论】:

以上是关于RxSwift 从一个创建多个 Observable的主要内容,如果未能解决你的问题,请参考以下文章

压缩后 RxSwift 丢失订单

使用 RXSwift 的警报消息的 UIViewController 扩展

在 RxSwift 中处理嵌套的 observables

如何在 RxSwift 中组合多个 Observable

可从多个 BehaviorRelay RxSwift 观察到

RxSwift 对一个 observable 的多个订阅