drive 只能从 MainThread 调用

drive can be only called from MainThread

在通过代码呈现新的 UIViewController 之后,在每个地方(在 viewModel 或 viewController 中)使用 .Drive,我得到这个错误: drive* family of methods can be only called from MainThread

这就是我展示新内容的方式 ViewController:

func goToVerifyPage() {

    let verifyVC = VerifyViewController()
    verifyVC.modalTransitionStyle = .flipHorizontal
    self.present(verifyVC, animated: true, completion: nil)
}

和内部验证ViewController:

override func viewDidLoad() {
        super.viewDidLoad()

        confirmVerifyCodeBTN.rx.tap
            .asDriver()
            .debounce(1)
            .filter({
                self.viewModel.signupEnabled
            })
            .drive(onNext:{  [weak self] _ in
                guard let verifyCode = self?.verificationCodeTF.text  else { return }
                self?.verifyActivateCode(verifyCode)
            }).disposed(by: disposeBag)
}

执行 .filter 行后显示的错误。

在之前的viewController(named loginViewController)中我使用了相同的代码但没有得到任何错误,唯一不同的是verifyViewController和loginViewController 是,使用故事板来呈现 ViewController(loginViewController).

更新: 当我使用此代码来呈现验证时ViewController 一切正常:

func goToVerifyPage() {
        DispatchQueue.main.async {
            let verifyVC = VerifyViewController()
            verifyVC.modalTransitionStyle = .flipHorizontal
            self.present(verifyVC, animated: true, completion: nil)
        }
}

我猜您是根据来自 URLSession 的网络请求结果调用 goToVerifyPage()。 URLSession 在后台线程上发出它的值,所以当你准备切换到主线程时,你应该有一个 .observeOn(MainThread.instance).