Runeaway3
Runeaway3

Reputation: 1449

Swift/Xcode: Why would PFUser.currentuser() not be returning nil if logged out?

I wanted to set up a conditional segue (in Swift/Xcode, with parse-server framework, hosted by Heroku) that will perform only if the current user has not logged out. It works appropriately if the user has not logged out. However, even if the user logs out, the segue will still run. Here is a basic set up of the code.

I call the user to log out via:

PFUser.logoutinbackground()

However, the segue:

If PFUser.currentuser() != nil {

//perform segue
}

will still perform the segue. Printing the PFUser.currentuser() to logs does not return nil. If I rewrite the conditional segue code to this:

If PFUser.currentuser()?.username != nil {
//perform segue
}

This works appropriately, returning PFUse.currentuser()?.username to the logs does return nil. Is there anything particularly wrong with leaving it like this (ie. will this cause any problems?). Also, why wouldn't it be return nil for the first version of the segue code? Does it have to do with the fact that I called PFUser.loginoutinbackground() as opposed to PFUser.logout() (the latter causes the app to crash for some reason)?

Upvotes: 3

Views: 287

Answers (1)

Brett Ponder
Brett Ponder

Reputation: 145

PFUser.logOut(); This logs the user out immediately. When you call PFUser.logoutinbackground() does not log the user out immediately.

PFUser.logoutinbackground()
if PFUser.currentuser() != nil {

//perform segue
}

logoutinbackground() is async. That is why the code above will not do what you think it should do. I always use PFUser.logOut();

Update

Make sure that you do not have this in your code

PFUser.enableAutomaticUser()

Upvotes: 2

Related Questions