本文介绍了RxSwift + MVVM + Coordinator 模式,如何依次等待协调器结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我有基本的协调员,基本上是这样的:

I have base coordinator which basically like so:

class Coordinator<Result> {
   // some coordinator class codes

   func coordinate(to: Coordinator<T>) -> Observable<T> {
       return coordinator.start()
   }

   func start() -> Observable<Result> {
      // return result
   }
} 

和 TestCoodinator1、TestCoordinator2、TestCoodinator3 像这样:

and TestCoodinator1, TestCoordinator2, TestCoodinator3 like so:

enum Status {
    case .success(data: NSData?)
    case .cancelled(error: NSError?)
    case .failed(data: NSData?)
}

class TestCoordinator1: Coordinator<Status> {
     // Init pass UINavigationController as rootViewController

     let testVC1 = TestViewController1()
     let testVM1 = TestViewModel1()
     testVC1.viewModel = testVM1         

     return testVM1.status // returns Observable<Status>
         .do(onNext: { [unowned self] _ in self?.rootViewController.popViewController(animated: true)
} 

我有主要协调员,基本上是这样的:

and I have main coordinator which basically like so:

class MainCoordinator: Coordinator<Void> {

    override func start() -> Observable<Void> {
        let mainVC = MainVC()
        let mainVM = MainVM()
        mainVC.viewModel = mainVM

        let testCoordinator1 = TestCoordinator1(in: rootViewController)
        let viewModel1 = ViewModel1()
        testCoordinator1.viewModel = viewModel1

        let testCoordinator2 = TestCoordinator2(in: rootViewController)
        let viewModel2 = ViewModel2()
        testCoordinator2.viewModel = viewModel2

        let testCoordinator3 = TestCoordinator3(in: rootViewController)
        let viewModel3 = ViewModel3()
        testCoordinator3.viewModel = viewModel3

        // viewModel.tests returns Observable that has three types of tests randomly generated
        // Observable.of("Test1", "Test2", "Test3")
        mainVM.tests ????

        return Observable.never()
    }
}

问题:

如何创建与 test1 协调的 observable,然后等待 test1 完成,然后对结果进行一些处理,然后才运行 test2,处理一些结果,然后运行 ​​test3.

How do I create observable that coordinates to test1 and wait test1 complete first, then do some processing on the result, then and only then test2 run, process some result, and then test3 run.

如果我这样做:

mainVM.tests
   .flatMap { test in 
       switch(test) {
           case "Test1":
                 return self.coordinate(to: testCoordinator1)
           case "Test2":
                 return self.coordinate(to: testCoordinator2)
           case "Test3":
                 return self.coordinate(to: testCoordinator3)
       }
   }
   .subscribe(onNext: { status in 
      self.processStatus(status)
   })

三个协调器同时运行

推荐答案

如果我理解正确,你可以通过 链接可观察对象:

If I understand you correctly, you can achieve that by chaining observables:

mainVM.tests
    .flatMapLatest { test -> Observable<...> in
        // do some things
        return self.coordinate(to: testCoordinator1)
    }
    .flatMapLatest { _ -> Observable<...> in
        // handle first coordinator
        return self.coordinate(to: testCoordinator2)
    }
    .flatMapLatest { _ -> Observable<...> in
        // handle second coordinator
        return self.coordinate(to: testCoordinator3)
    }
    .flatMapLatest { _ -> Observable<...> in
        // handle third coordinator
    }
    .subscribe(onNext: { status in
        self.processStatus(status)
    })

这篇关于RxSwift + MVVM + Coordinator 模式,如何依次等待协调器结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 13:06