gpt4 book ai didi

swift - Rx swift : how to get rid of callback hells of subscribe events and network request?

转载 作者:行者123 更新时间:2023-11-28 13:42:03 27 4
gpt4 key购买 nike

RxSwift:如何摆脱订阅事件和网络请求的回调 hell ?

我是新来的Swifter,这是我新公司的代码。

下面的代码非常连线。

逻辑很简单。 BoutiqueOutput 有一个 requestCommand,它是 PublishSubject。它用于做一些刷新的事情(一次又一次地请求网络)。

回调 hell 就在其中。网络层 Moya 以 RxSwift 方式使用。

RxSwift 的新手,我不知道重构它的模式。

如何正确使用 filter/map/combine 运算符?

这是用RxSwift封装的模型


import RxSwift
import RxCocoa
import RxDataSources
import MJRefresh
import Moya
import MoyaMapper


// this is the Moya Network Provider
let Provider = MoyaProvider<Router>(endpointClosure: EndpointClosure, requestClosure: requestClosure, plugins: [networkPlugin, MoyaMapperPlugin(NetParameter())], trackInflights: false)


struct BoutiqueOutput: OutputRefreshProtocol {

var refreshStatus = Variable<RefreshStatus>(.none)

let sections: Driver<[CategoryLeftSection]>

let requestCommand = PublishSubject<Bool>()
init(sections: Driver<[CategoryLeftSection]>) {
self.sections = sections
}
}


class CategoryViewModel: NSObject {

let vmDatas = Variable<[ParentItem]>([])

func transform() -> BoutiqueOutput {

let tempSections = vmDatas.asObservable().map({ (sections) -> [CategoryLeftSection] in
return [CategoryLeftSection(items: sections)]
}).asDriver(onErrorJustReturn: [])

let output = BoutiqueOutput(sections: tempSections)
output.requestCommand.subscribe(onNext:{[weak self] _ in
guard let self = self else { return }
Provider.rx.cacheRequest(.baseUIData).subscribe( onNext:{ result in
// do some UI
if result.statusCode == 200 || result.statusCode == 230 {
// do something business
}

}).disposed(by: self.rx.disposeBag)
}).disposed(by: rx.disposeBag)
return output
}
}


模型是这样应用的

private var vmOutput: BoutiqueOutput?


override func viewDidLoad() {
super.viewDidLoad()
self.vmOutput = viewModel.transform()
boundTableViewData()
// ...
}


private func boundTableViewData() {

let dataSource = RxTableViewSectionedReloadDataSource<CategoryLeftSection>( configureCell: { [weak self] ds, tv, ip, item in
// ...
}


vmOutput!.sections.asDriver().drive(self.leftMenuTableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)

// ...

vmOutput!.requestCommand.onNext(true)

}



private func requestErrorRefresh() {
// ...
if isNetworkConnect {
boundTableViewData()
vmOutput!.requestCommand.onNext(true)
}
}


private func noNetworkRefresh() {
// ...
if isNetworkConnect {
boundTableViewData()
vmOutput!.requestCommand.onNext(true)
}
}

最佳答案

我使用combineLatest 运算符来解决这个问题。

代码如下:

let output = BoutiqueOutput(sections: temp_sections)
Observable.combineLatest(output.requestCommand, Provider.rx.cacheRequest(.baseUIData)).subscribe({ [weak self] ( result: Event<(Bool, Response)>) in
guard let self = self else { return }
switch result{
case .next(let response):
// do some UI
if result.statusCode == 200 || result.statusCode == 230 {
// do something business
}
default:
break
}
}).disposed(by: rx.disposeBag)

combineLatest:每当任何可观察序列产生一个元素时,将指定的可观察序列合并到一个可观察的元组序列中。

将两个不相关的事件结合在一起,做一些共同的事情是很好的。

By the way: I think it two days and don't know how to solve it . And I listed systematically and posted here, after some time, I figured it out. Interesting experience.

关于swift - Rx swift : how to get rid of callback hells of subscribe events and network request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55886840/

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