0

I've created a user class which has a Parse PFUser() object in it and a string for error_messages.

In the user class is a signup function which will use the PFUser object and perform signUpInBackgroundWithBlock().

In that function it should set a flag notifying the main view controller that an error occurred if one does as well as set the error_message string in the User object with the error message passed back from PFUser.

However what happens is the function doesn't finish executing once an error occurs for example if an incorrect email format is entered such as aaaa.com instead of aaa@a.com the function won't return and set the flag instead the error message passed from PFUser is just displayed in the console window.

I've spent a few days now trying everything imaginable to set the flag and the error message in the user class but I can't figure it out.

Here is the class code

class User {
var user_db_obj = PFUser()
var error_message = "Please try again later"
//var user_name: String
//var pass_word: String


//Constructor for User object
init(user: String, pass: String){
    user_db_obj.username = user
    user_db_obj.email = user
    user_db_obj.password = pass
}


//This function signs a user up to the database
func signUp() -> Bool {
    var error_in_sign_up: Bool = true

    user_db_obj.signUpInBackgroundWithBlock {(succeeded: Bool?, error: NSError?) -> Void in

        //stop spinner and allow interaction events from user
        activityIndicator.stopAnimating()
        UIApplication.sharedApplication().endIgnoringInteractionEvents()

        if error == nil{
            error_in_sign_up = false
            //sign up successful
        }
        else{
            let error_string = error!.userInfo["error"] as? String
            self.error_message = error_string!
        }
    }
    if error_in_sign_up == true{
        return false
    }
    else{
        return true
    }
}

Here is the view controller code that calls the signup function from User class.

    //Action for signup button
@available(iOS 8.0, *)
@IBAction func signup_btn(sender: AnyObject) {

    if email_tf.text!.isEmpty || pass_tf.text!.isEmpty {
        //if email or password field blank display error message
        displayAlert("Error in form", msg: "Please enter a username and password")
    }

    else{ //perform actual signup/login if email and password supplied

        //Display spinner while database interaction occuring and ignore user interactions as well
        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        UIApplication.sharedApplication().beginIgnoringInteractionEvents()

        let theUser = User(user: email_tf.text!, pass: pass_tf.text!)

        //sign up
        if signup_mode == true{
            if theUser.signUp() == false{
                displayAlert("Failed SignUp", msg: theUser.error_message)
            }
        }

        //login
        else{
            if theUser.login() == false{
                displayAlert("Failed Login", msg: theUser.error_message)
            }
        }
    }

}
jtbandes
  • 115,675
  • 35
  • 233
  • 266
momonkey
  • 41
  • 1
  • 7

1 Answers1

0

the problem is that function signUpInBackgroundWithBlock doesnt run on mainthread, if you want to keep this functions you would have to register notification and then listen when it is successful or not in the other viewController... something like this

Community
  • 1
  • 1
Mazel Tov
  • 2,064
  • 14
  • 26