Reputation: 1413
I am looking at the Parse documentation, and there is this interesting function I would like to use to make validate for signing up users if the username and email provided are registered (taken) or not. The documentation provides example for the following:
var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)
var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)
var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
my version of it is:
/*Check username and email not taken*/
let usernameInputCheck = PFQuery(className:"_User")
usernameInputCheck.whereKey("appUsername", equalTo: self.usernameTxtField.text!)
let emailInputCheck = PFQuery(className:"_User")
emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)
let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
I want to be able to check if email or username is taken, and if yes set a flag so that I can set message to user saying for example: "Email provided is already registered" or "Username is not available, try a different one".
Thanks for your help in advance.
Update: Here is the link to Parse docs: https://parse.com/docs/ios/guide#queries-compound-queries
Upvotes: 0
Views: 389
Reputation: 1413
Workaround the problem:
let usernameInputCheck = PFQuery(className:"_User") usernameInputCheck.whereKey("appUsername", equalTo: self.usernameTxtField.text!)
let emailInputCheck = PFQuery(className:"_User")
emailInputCheck.whereKey("email", equalTo:self.emailTxtField.text!)
let query = PFQuery.orQueryWithSubqueries([usernameInputCheck, emailInputCheck])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
let checkResult = self.emailOrUsernameIsTaken(results!)
if(checkResult != 0 && checkResult == 1)
{
//username taken
}else if (checkResult != 0 && checkResult == 2){
//email taken
}
}
}
and here is the function:
func emailOrUsernameIsTaken(results: [PFObject])->Int
{
/*Check if username is taken or if email is taken*/
var preferenceTaken: Int = 0
if(results[0]["email"] as! String != "" && results[0]["email"] as! String == self.userObject.email!)
{
preferenceTaken = 1
}else if(results[0]["appUsername"] as! String != "" && results[0]["appUsername"] as! String == self.userObject.username!){
preferenceTaken = 2
}
return preferenceTaken
}
Upvotes: 0
Reputation: 2488
Here is some objective-c that does the trick, which you could easily adapt to Swift.
PFQuery * nameQuery = [PFQuery queryWithClassName:@"user"]
[nameQuery whereKey:"username" equalTo:username];
PFQuery * emailQuery = [PFQuery queryWithClassName:@"user"];
[emailQuery whereKey:"email" equalTo:email];
PFQuery * query = [PFQuery orQueryWithSubqueries:@[nameQuery, emailQuery]
[query countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
if(number > 0) {
//Email address OR username is already in use.
}
}];
Upvotes: 2