gpt4 book ai didi

ios - RxSwift 从一个 Observable 创建多个 Observable

转载 作者:行者123 更新时间:2023-11-30 11:16:40 24 4
gpt4 key购买 nike

如何共享一个 API 调用?例如,我有以下模型类。这些属性将绑定(bind)到不同的 UI 组件。我正在 ViewController 中实现这个。但是,我认为我不应该将模型暴露给 View 层。相反,我应该为 ViewController 提供两个要绑定(bind)的 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)

最佳答案

我有一个像这样的不成熟的解决方案。因此,我可以将这两个 Observable 绑定(bind)到 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)
}
}

关于ios - RxSwift 从一个 Observable 创建多个 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51681458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com