gpt4 book ai didi

ios - RxSwift : tap a button to do a request with Alamofire, 如何让它工作?

转载 作者:行者123 更新时间:2023-12-01 16:06:36 32 4
gpt4 key购买 nike

我有一些 UI rx 按钮点击,我想将它连接到网络 rx 部分。

这是 Rx btn tap 部分:

btn.rx.tap.take(1).flatMap {  [unowned self]  () -> Observable<Bool>  in
// ...
return NetManager.standard.request()
}.debug().do(onNext: { _ in
print("The data flows here")
}).flatMapLatest { (echo: Bool) -> Observable<Bool> in
if echo{
if let app = UIApplication.shared.delegate as? AppDelegate{
app.goHome()
}
return Observable.of(true)
}
else{
return Observable.of(false)
}
}.subscribe(onNext: { (result) in
// ... , do sth with result
}).disposed(by: rx.disposeBag)

这是 Alamofire 的网络请求部分:

class NetManager: NSObject {

static let standard = NetManager()
var dataRequest: DataRequest?

func request() -> Observable<Bool> {
return Observable<Bool>.create{ (observer) -> Disposable in
let data = // ...
// ...
self.dataRequest = AF.request(aURL, method: HTTPMethod.post, parameters: data, encoder: JSONParameterEncoder.prettyPrinted, headers: aHead).response { (response: DataResponse<Data?>) in
self.hud?.hide()
if let data = response.data{
observer.onNext(true)
// ....
}
else{
observer.onNext(false)
}
}
observer.onCompleted()
return Disposables.create{}
}
}

}

这是调试信息:

Controller.swift:141 (viewDidLoad()) -> subscribed

Controller.swift:141 (viewDidLoad()) -> Event completed

Controller.swift:141 (viewDidLoad()) -> isDisposed

结果是,do(next) 部分似乎还没有被调用。因为控制台中没有print


问题是,如何让 rx 逻辑工作?


这是我尝试过的:

btn.rx.tap.flatMap 转为btn.rx.tap.take(1).flatMap

因为来自 this issue

if a flatMap completion would terminate the outer stream, it would mean that no more button taps would be registered in your stream, which don't really make sense.


来自this question

  btn.rx.tap.map { () -> Observable<Bool> in

return NetManager.standard.request()
}.flatMapLatest { (observer: Observable<Bool>) -> Observable<Bool> in

// of cource, I can subscribe to observer
/*
observer.subscribe(onNext: { (echo) in

})
*/


// how to get the event element elegantly.

return Observable.of(false)
}.subscribe(onNext: { (_) in

}).disposed(by: rx.disposeBag)

如何更优雅地做到这一点。

最佳答案

这里的主要问题是,在执行网络请求时,您没有等待网络调用响应。所以 observer.onComplete()observer.onNext()

之前触发
class NetManager: NSObject {

static let standard = NetManager()
var dataRequest: DataRequest?

func request() -> Observable<Bool> {
return Observable<Bool>.create { (observer) -> Disposable in
let data = // ...
// ...
self.dataRequest = AF.request(aURL, method: HTTPMethod.post, parameters: data, encoder: JSONParameterEncoder.prettyPrinted, headers: aHead).response { (response: DataResponse<Data?>) in
self.hud?.hide()
if let data = response.data {
observer.onNext(true)
// ...
}
else {
observer.onNext(false)
}
observer.onComplete() // Moving it here instead will wait for the response
}
return Disposables.create { }
}
}
}

此时,您的代码应该可以正常工作,但您仍然将一件非常简单的事情复杂化了。

这是要做的事情:

btn.rx.tap
.flatMap { _ in
return NetManager.standard.request()
}
.subscribe(onNext: { (success) in
if success {
if let app = UIApplication.shared.delegate as? AppDelegate {
app.goHome()
}
}
})
.disposed(by: rx.disposeBag)

关于ios - RxSwift : tap a button to do a request with Alamofire, 如何让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58834262/

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