我正在使用UIViewcontroller
处理RXSwift
的alertmessage扩展程序。
// UIViewController+Alertmessage
public func observeAlertMessageSignal(alert: Observable<String>) {
let disposeBag = DisposeBag()
alert.observeOn(MainScheduler.instance)
.subscribe(onNext: { message in
self.alertCustomTransitionDelegate = CustomTransitioningDelegate()
let viewModel = AlertViewModel(message: message)
let storyboard = UIStoryboard(name: "Alert", bundle: nil)
let viewController = storyboard.instantiateInitialViewController() as! AlertViewController
viewController.viewModel = viewModel
viewController.delegate = self
viewController.modalPresentationStyle = .custom
viewController.transitioningDelegate = self.alertCustomTransitionDelegate
self.present(viewController, animated: true, completion: nil)
}).addDisposableTo(disposeBag)
}
// ViewModel
--> Declaration of alertObservable in viewModel
var alertObservable = PublishSubject<String>()
--> usage of observable:
transportResponse = active.asObservable()
.filter { $0 }
.flatMap { _ in
return webservice.loadTransport()
}
.catchError { [weak self] e in
if let moyaError = e as? MoyaError {
if case .underlying(let error) = moyaError {
let formattedError = error as NSError
let message = NSError.localizedDescription(error: formattedError)
self?.alertObservable.onNext(message)
}
}
self?.isLoadingObservable.onNext(false)
throw e
}
.map { response in
return [TransportData(items: response)]
}
//ViewController:
--> code inside viewDidLoad
self.observeAlertMessageSignal(alert: viewModel.alertObservable)
调用observeAlertMessageSignal
函数但未显示我的AlertViewController。对此有何帮助?
谢谢!
答案 0 :(得分:5)
您将disposeBag声明为方法变量。当方法调用结束时,disposebag将被取消分配,并且您的订阅也将被处理。你可以简单地将一个disposebag传递给方法,并将那个disposebag放在VC中