登录 Realm Mobile Platform 后弹出视图控制器

Pop view controller after signing in to Realm Mobile Platform

我在我的 Realm iOS 应用程序中使用 Realm Mobile Platform,我试图在用户登录后弹出视图控制器。这是我现在的代码:

let usernameCredentials = SyncCredentials.usernamePassword(username: emailField.text!, password: passwordField.text!, register: false)
let serverURL = URL(string: "http://45.55.220.254:9080")
SyncUser.logIn(with: usernameCredentials, server: serverURL!){ user, error in
    if user != nil{
        print("signed in")
        MyRealm.copyToSyncedRealm()                
        self.navigationController?.popToRootViewController(animated: true)
    }else if let error = error{
        self.loginButton.setTitle("Log in", for: .normal)
        let alert = UIAlertController(title: "Error", message: String(describing: error), preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { void in

        }))
        self.present(alert, animated: true)
   }
 } 

但是,当我登录时,视图控制器没有弹出,当我单击导航项上的后退按钮时,应用程序冻结。知道为什么会这样吗?

传递给 SyncUser.logIn(with:server:) 的回调在后台队列上调用。由于只有从主线程调用 UIKit 才是安全的,因此您需要将工作分派到主线程。

SyncUser.logIn(with: usernameCredentials, server: serverURL!) { user, error in
    DispatchQueue.main.async {
        if user != nil {
            // Success…
        } else if let error = error {
            // Error…
       }
    }
 }