3
我觉得我已经开始了RxSwift的悬念 - 但是我刚刚遇到了障碍。 这是我为演示制作的一个对象(我在发布之前已经简化了它)。我的问题是,当上传过程中出现网络错误时,所有订阅都会被处理掉。所以当我再次点击rightBarButtonItem
时,没有任何反应。错误后重新订阅Observable
什么是正确/更好的建模方式?我不确定我是否正确使用了PublishSubject
!
let activityIndicator = ActivityIndicator()
let disposeBag = DisposeBag()
let rx_upload = PublishSubject<Void>()
let rx_progress = PublishSubject<RxProgress>()
let rx_uploadComplete = PublishSubject<Look>()
override init() {
super.init()
activityIndicator
.drive(UIApplication.sharedApplication().rx_networkActivityIndicatorVisible)
.addDisposableTo(disposeBag)
let upload = rx_upload
.debug("Upload")
.flatMapLatest { [unowned self] -> Observable<(JSON?, RxProgress)> in
return self.upload()
}
.share()
upload
.map { $0.1 }
.debug("Upload Progress")
.bindTo(rx_progress)
.addDisposableTo(disposeBag)
upload
.filter { $0.0 != nil }
.map { Post(jsonData: $0.0!) }
.filterNil()
.debug("Upload Complete")
.bindTo(rx_uploadComplete)
.addDisposableTo(disposeBag)
}
func upload() -> Observable<(JSON?, RxProgress)> {
// ...
}
而且在ViewController.swift
...
self.navigationItem.rightBarButtonItem?.rx_tap
.bindTo(postUploader.rx_upload)
.addDisposableTo(disposeBag)