我想设置一个条件 segue(在 Swift/Xcode 中,使用解析服务器框架,由 Heroku 托管),只有当当前用户没有注销时才会执行。如果用户没有注销,它会正常工作。但是,即使用户注销,segue 仍会运行。这是代码的基本设置。
我通过以下方式调用用户注销:
PFUser.logoutinbackground()
但是,转场:
If PFUser.currentuser() != nil {
//perform segue
}
仍然会执行 segue。将 PFUser.currentuser() 打印到日志不会返回 nil。如果我将条件 segue 代码重写为:
If PFUser.currentuser()?.username != nil {
//perform segue
}
这工作正常,返回 PFUse.currentuser()?.username 到日志确实返回 nil。像这样保留它有什么特别不对的地方吗(即这会导致任何问题吗?)。另外,为什么第一个版本的 segue 代码不返回 nil?这是否与我调用 PFUser.loginoutinbackground() 而不是 PFUser.logout() (后者导致应用程序因某种原因崩溃)这一事实有关?
最佳答案
PFUser.logOut();
这会立即注销用户。当您调用 PFUser.logoutinbackground()
时,不会立即注销用户。
PFUser.logoutinbackground()
if PFUser.currentuser() != nil {
//perform segue
}
logoutinbackground()
是异步的。这就是为什么上面的代码不会做你认为它应该做的事情。我总是使用 PFUser.logOut();
更新
确保你的代码中没有这个
PFUser.enableAutomaticUser()
关于ios - 如果注销,Swift/Xcode : Why would PFUser. currentuser() 不会返回 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38707419/