I have property of a PFUser object that needs to be unique.
For example the default email field is unique and throws an exception with an alert when the criteria not met.
How can I set this field to be unique ?
I have property of a PFUser object that needs to be unique.
For example the default email field is unique and throws an exception with an alert when the criteria not met.
How can I set this field to be unique ?
To clarify, parse enforces email uniqueness on the User model, but you'd like to enforce uniqueness on some other field, say a made-up one like, employeeId.
To do this client-side, do a query first to make sure the condition is met:
NSString *employeeId = @"hopefully this is unique";
PFQuery *query = [PFUser query];
[query whereKey:@"employeeId" equalTo:employeeId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects.count) {
NSLog(@"shucks, the employeeId isn't unique");
} else {
PFUser *user = [PFUser user];
user.employeeId = employeeId;
// setup the rest of user
[user signUpInBackgroundWithBlock:(BOOL success, NSError *error) {
if (success) NSLog(@"yay! new user);
}];
}
}];
To do this server-side, It might be possible to do basically the same thing in a beforeSave hook on the user model.
Parse.Cloud.beforeSave(Parse.User, function(request, response) {});
objects.count == 0, it will create the user and sign them up. From the time it takes to make the sign up request and actually create the user, another request could come in that creates a user with the same employeeId. Because the user hasn't been created yet this other request will also see objects.count == 0. Both requests succeed, you end up with duplicate employee ids. Moving it into Cloud Code helps because you reduce the threshold time, but still another request could come in while that function is running and result in non-uniqueness.