【发布时间】:2016-04-25 16:18:03
【问题描述】:
我觉得我开始掌握 RxSwift 的窍门 - 但是我刚刚遇到了障碍。
这是我为演示构建的对象(我在发布到 SO 之前对其进行了简化)。我的问题是,当上传过程中出现网络错误时,所有订阅都会被处理掉。所以当我再次点击rightBarButtonItem 时,什么也没有发生。
对此建模的正确/更好的方法是什么?我不确定我是否正确掌握了PublishSubjects 的用法!
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)
【问题讨论】:
标签: ios swift rx-swift reactivex