ios - 协调器的 RxSwift 并发问题
问题描述
我想做什么:
- 介绍VC1
- 当 VC1 关闭时,当前 VC2
问题:
- 当 VC1 关闭时,VC2 不存在
肮脏的修复:放置毫秒延迟。它解决了问题,但想知道为什么会发生
解释:当 VC1 关闭时我收到 viewDidDissapear 事件,因此我可以展示 VC2
如果您需要更多详细信息,请询问。
代码:
class ViewModel {
let coordinator = Coordinator()
struct Input {
let itemSelected: Driver<IndexPath>
}
struct Output {
let presentVC1: Driver<Void>
let presentVC2: Driver<Void>
}
func transform(input: Input) -> Output {
let navigateToVC1 = input.itemSelected
.flatMap { [coordinator] in
return coordinator.transition(to: Scene.VC1)
}
let navigateToVC2 = navigateToVC1
.delay(.milliseconds(1))
.flatMap { [coordinator] in
return coordinator.transition(to: Scene.VC2)
}
return Output(presentVC1: presentVC1, presentVC2: presentVC2)
}
协调员代码:
func transition(to scene: TargetScene) -> Driver<Void> {
let subject = PublishSubject<Void>()
switch scene.transition {
case let .present(viewController):
_ = viewController.rx
.sentMessage(#selector(UIViewController.viewDidDisappear(_:)))
.map { _ in }
.bind(to:subject)
currentViewController.present(viewController, animated: true)
return subject
.take(1)
.asDriverOnErrorJustComplete()
}
解决方案
viewDidDisappear
在视图控制器完全关闭之前调用该方法。在调用回调之前,您不应尝试呈现第二个视图控制器dismiss
。
无论您在何处关闭视图控制器,请改用以下内容,并且在 observable 发出下一个事件之前不要显示下一个视图控制器。
extension Reactive where Base: UIViewController {
func dismiss(animated: Bool) -> Observable<Void> {
Observable.create { [base] observer in
base.dismiss(animated: animated) {
observer.onNext(())
observer.onCompleted()
}
return Disposables.create()
}
}
}
我建议你考虑使用我的 Cause-Logic-Effect 架构,其中包含正确处理视图控制器呈现和解除所需的一切。
https://github.com/danielt1263/CLE-Architecture-Tools
部分界面如下:
/**
Presents a scene onto the top view controller of the presentation stack. The scene will be dismissed when either the action observable completes/errors or is disposed.
- Parameters:
- animated: Pass `true` to animate the presentation; otherwise, pass `false`.
- sourceView: If the scene will be presented in a popover controller, this is the view that will serve as the focus.
- scene: A factory function for creating the Scene.
- Returns: The Scene's output action `Observable`.
*/
func presentScene<Action>(animated: Bool, overSourceView sourceView: UIView? = nil, scene: @escaping () -> Scene<Action>) -> Observable<Action>
extension NSObjectProtocol where Self : UIViewController {
/**
Create a scene from an already existing view controller.
- Parameter connect: A function describing how the view controller should be connected and returning an Observable that emits any data the scene needs to communicate to its parent.
- Returns: A Scene containing the view controller and return value of the connect function.
Example:
`let exampleScene = ExampleViewController().scene { $0.connect() }`
*/
func scene<Action>(_ connect: (Self) -> Observable<Action>) -> Scene<Action>
}
struct Scene<Action> {
let controller: UIViewController
let action: Observable<Action>
}
该connect
函数是您的视图模型,当它的 Observable 完成或它的订阅者处置时,视图控制器将自动关闭。
该presentScene
功能是您的协调员。它处理场景的实际呈现和解除。当您关闭并呈现一个新场景时,它将正确处理等待,直到前一个视图控制器被关闭,然后再呈现下一个。
推荐阅读
- wpf - 有没有办法在另一个程序集中使用资源文件?
- oracle - 如何将 Oracle 查询转换为条件查询?
- c - 确定我的代码的时间复杂度是多少?
- c# - C# LazyCache 并发字典垃圾回收
- ibm-cloud - 在 watson 控制台上哪里可以找到工作区 ID?
- bash - AWK 以相反的顺序读取文件并打印指定的列
- javascript - 用对象解构替换 Javascript
- filebeat - Filebeat 在 close_eof + --once 上未完成
- gradle - 在 Gradle 中应用插件抛出 NullPointerException
- javascript - Highcharts 使用 React TypeScript 根据数据值填充地图