Hi I'm working on customizing the Parse PFLogInViewController so that the logInButton will actually function as a sign up and login button by first checking to see if the username already exists. If it doesn't, then the user will be created. If it does, then the user will be logged in. I'm trying to run this query and check inside the shouldBeginLogInWithUsername function, but I think I'm having trouble because of the asynchronous query. I've been trying to figure it out for hours with no luck. Here's my code:
func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {
var bool = false
var query = PFUser.query()
query!.whereKey("username", equalTo: username)
query?.findObjectsInBackgroundWithBlock({ (users, error) -> Void in
if let users = users {
if users.count == 0 {
var user = PFUser()
user.username = username
user.password = password
user.signUpInBackground()
} else {
return bool = true
}
} else {
return bool = true
}
})
return bool
}
As you can see, I need my Boolean variable bool to change from false to true if the username exists and the user needs to be logged in. I just can't figure out how to get around the asynch request.
Any help would be much appreciated!
Since the call is async you can't expect to be able to return true or false directly from your function. You need to pass a closure to the function, which will be called when the async operation completes. Sort of like this (this is totally untested, but hopefully you get the idea):
Use it like this, sort of: