0

I use the following code to identify if a PFUser already exists:

let findExistingUserQuery = PFQuery(className: "_User")
                    findExistingUserQuery.whereKey("email", equalTo: EmailTextField.text!)
                    findExistingUserQuery.findObjectsInBackgroundWithBlock{ (objects, error) -> Void in
                        if(error == nil){
                            print(objects![0])
                            if(objects!.count > 0){
                                let existingAlert = UIAlertController(title: "Alert", message: "A user with this email already exists", preferredStyle: .Alert)
                                existingAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
                                self.presentViewController(existingAlert, animated: true, completion: nil)

                                self.Spinner.hidden = true
                                self.Spinner.stopAnimating()

                                print("exists")
                            }else{
                                print("available")
                                //temporarily save user info
                                user.username = self.EmailTextField.text
                                user.email = self.EmailTextField.text
                                user["PhoneNumber"] = self.PhoneNumberTextField.text
                                phone = self.PhoneNumberTextField.text!

                                //segue to Create Profile Screen
                                self.performSegueWithIdentifier("ToCreateProfileSegue", sender: nil)
                                self.Spinner.hidden = true
                                self.Spinner.stopAnimating()
                            }
                        }

                    }

However, for some reason, the else is never called even if there is no user. The first part of the call works and can identify when a user is already loaded in the database. Any suggestions?

Gyro Technologies
  • 47
  • 1
  • 2
  • 11
  • Two things: One - Parse does this automatically for you when a user signs up (it checks if the username and/or email matches any other account). Two - I've actually been playing with this results problem too. If the query is sent out and the error == nil and objects are returned, Swift thinks no problem occurred. I believe the result that would get is no existing users have that email address is ([]). So I think you would need to check if the results == ([]) or == nil. I would just play around with it to detect is the results returned includes no valuable information. – Dan Levy Apr 10 '16 at 14:48
  • I am aware of the built in function. Problem is, I am collecting user info over several view controllers. – Gyro Technologies Apr 10 '16 at 14:52
  • I actually do the same thing! On the first ViewController, collect the users username and password (and a password confirm) -> send that data to Parse. Then, when you are saving data to parse for that user, you can call PFUser.currentUser()!.saveInBackground and it will check to make sure the email is not used for another account. If it isn't, success == true and it will save it to the user's account that you just created. – Dan Levy Apr 10 '16 at 15:00
  • yes but what if the user doesn't complete the sign up process. Won't you then have created a user with missing information? – Gyro Technologies Apr 10 '16 at 15:04
  • Right. The way I handled it: If I get the user's username and password, they can still log in without a problem. On the user's account page, if information is missing, I have a red button pop up telling them to add their information. If you want to go further with it, you could check for, say, PFUser.currentUser()!.email to see if it is nil...if it is, you could prevent them from doing certain things in the app until they provide their email. – Dan Levy Apr 10 '16 at 15:11
  • Once Parse gets emails up and running on Parse-Server, you will be able to send a user confirmation email. If the user doesn't complete the signup process, you can check when the user logs in if the email informed key is equal to true or false. If false, you can display a page that tells them to confirm their email or resend the email. When the user confirms the email and comes back to the app, have a button the refreshes the page to check for true or false. If it is true, segue them to the main view controller. – Dan Levy Apr 10 '16 at 15:11

3 Answers3

0

Maybe because Object not found is an error and you need to call that block in the else of error part instead of count check.

Does your code print(objects![0]) logs the output when no user is found.

Rahul Katariya
  • 3,528
  • 3
  • 19
  • 24
0

Two things: One - Parse does this automatically for you when a user signs up (it checks if the username and/or email matches any other account). Two - I've actually been playing with this results problem too. If the query is sent out and the error == nil and objects are returned, Swift thinks no problem occurred. I believe the result that would get is no existing users have that email address is ([]). So I think you would need to check if the results == ([]) or == nil. I would just play around with it to detect is the results returned includes no valuable information.

A few other things I noticed (only pertains if this is going to become a production app)

1) Check if objects == nil, then check else if error != nil, then perform an else statement. If there is an error returned from Parse, you can display that error using error!.localizedDescription

2) The sender in performSegue should be Self, referring to the fact that self is the view controller you are on now and that view controller is the sender (the one performing the segue).

3) Make sure that, when the user is signing up or logging in, you clean up the characters to get rid of any spaces. Also, here is how to detect if the email is actually an email.

Community
  • 1
  • 1
Dan Levy
  • 3,931
  • 4
  • 28
  • 48
0
//query users for matching email
                    let findExistingUserQuery = PFUser.query()
                    findExistingUserQuery!.whereKey("email", equalTo: EmailTextField.text!)
                    findExistingUserQuery!.findObjectsInBackgroundWithBlock {
                        (objects: [PFObject]?, error: NSError?) -> Void in

                        if error == nil {
                            // The find succeeded.
                            print("Successfully retrieved \(objects!.count) users.")
                            // Do something with the found objects
                            if (objects!.count > 0) {
                                print("user exists")

                                let existingAlert = UIAlertController(title: "Alert", message: "A user with this email already exists", preferredStyle: .Alert)
                                existingAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
                                self.presentViewController(existingAlert, animated: true, completion: nil)

                                self.Spinner.hidden = true
                                self.Spinner.stopAnimating()
                            }else{
                                print("no user found")
                                //temporarily save user info
                                user.username = self.EmailTextField.text
                                user.email = self.EmailTextField.text
                                user["PhoneNumber"] = self.PhoneNumberTextField.text
                                phone = self.PhoneNumberTextField.text!

                                //segue to Create Profile Screen
                                self.performSegueWithIdentifier("ToCreateProfileSegue", sender: nil)
                                self.Spinner.hidden = true
                                self.Spinner.stopAnimating()
                            }
                        } else {


            // Log details of the failure
                        print("Error: \(error!) \(error!.userInfo)")
                    }
                }

This code now works. I changed the findObjectsInBackgrounfWithBlock function slightly

Gyro Technologies
  • 47
  • 1
  • 2
  • 11