swift - performSegue无法在RxSwift Observable订阅中工作

时间:2018-06-03 17:07:31

标签: ios swift rx-swift

我有以下代码:

loginViewModel.facebookLogin
.asObservable()
subscribe() { [unowned self] facebookLogin in
     if let isLoggedIn = facebookLogin.element?.isLoggedIn {
         if isLoggedIn {
              elf.performSegue(withIdentifier: "toRestaurantSelect", sender: self)
              }
      }

     if let didLoginFail = facebookLogin.element?.didLoginFail {
         self.errorLabel.isHidden = !didLoginFail
     }
  }
.disposed(by: disposeBag)

facebookLogin是一个变量,在用户登录时会更新。但是,不会调用performSegue(条件为true)。奇怪的是,如果我在模拟器中打开慢动画,就会执行segue。当关闭慢动画时,segue不会执行(Facebook登录工作)。任何帮助表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您应该使用主线程来使其正常工作

    loginViewModel.facebookLogin
        .asObservable()
        .subscribe() { [unowned self] facebookLogin in
            if let isLoggedIn = facebookLogin.element?.isLoggedIn {
                if isLoggedIn {


                    DispatchQueue.main.async {
                        self.performSegue(withIdentifier: "toRestaurantSelect", sender: self)

                    }



                }
            }

            if let didLoginFail = facebookLogin.element?.didLoginFail {
                self.errorLabel.isHidden = !didLoginFail
            }
        }
        .disposed(by: disposeBag)

答案 1 :(得分:0)

使用主调度程序进行观察:

loginViewModel.facebookLogin
.asObservable()

// Switch to the main scheduler
.observeOn(MainScheduler.instance)


subscribe() { [unowned self] facebookLogin in
     if let isLoggedIn = facebookLogin.element?.isLoggedIn {
         if isLoggedIn {
              elf.performSegue(withIdentifier: "toRestaurantSelect", sender: self)
              }
      }

     if let didLoginFail = facebookLogin.element?.didLoginFail {
         self.errorLabel.isHidden = !didLoginFail
     }
  }
.disposed(by: disposeBag)