-1

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!

Matt Schwartz
  • 143
  • 2
  • 8

1 Answers1

0

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):

func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String, callback: (usernameExists : Bool) -> Void) -> Void {
    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.signUp()
               callback(usernameExists: false)
            } else {
               callback(usernameExists: true)
            }
        } else {
           callback(usernameExists: false)
        }
    })
}

Use it like this, sort of:

logInViewController(viewController, shouldBeginLogInWithUsername: "", password: "", callback: { (usernameExists) -> Void in
   //Do your login logic here
   if (usernameExists) {

   } else {

   }
})
rodskagg
  • 3,827
  • 4
  • 27
  • 46
  • Hi @andlin thanks for the quick response! I tried implementing the code you showed, but it didn't quite get the outcome I'm looking for. Sorry, I'm very new to Swift/Parse and probably did a bad job explaining what I'm trying to accomplish. If the query comes back and doesn't find the user, I want to save the user and return false for the logInViewController function so it doesn't try to log them in before they're created. If the query comes back and finds the user, it should return true to the logInViewController function so they get logged in. – Matt Schwartz Dec 06 '15 at 20:16
  • looking at the code you provided, I'm thinking maybe it has something to do with how you have the logInViewController function returning a Void. I think I need this to actually be a Bool that is either true or false based on whether or not it goes through the `users.count == 0` test. I can't seem to figure out how to structure that though. Thanks again! – Matt Schwartz Dec 06 '15 at 20:25
  • You get the bool you're looking for as a parameter to the callback closure. You then check the value of the usernameExists parameter and do your login in the closure. Do you want me to give you a code example? – rodskagg Dec 06 '15 at 20:57
  • that makes sense, but when I tried to structure something like that I couldn't seem to get it right. If you have an example on hand, I'd greatly appreciate it. Thanks again! – Matt Schwartz Dec 06 '15 at 21:18
  • If the PFUser.signUpInBackground is causing trouble as well, since I suppose it's async, you can use PFUser.signUp instead. – rodskagg Dec 06 '15 at 21:35