4

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 ?

2
  • 1
    My understanding is that this is not really possible, even with Cloud Code. Commented Jan 20, 2014 at 16:27
  • 1
    This question is already anwswered in parse.com Commented Feb 4, 2015 at 15:25

1 Answer 1

5

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) {});
Sign up to request clarification or add additional context in comments.

6 Comments

I guess I was asking if there was a parse method that did like [pfobject addUniqueProperty:@"uniqueProp" forKey@"uniqueProp"] . I guess the answer is no.
Right. I don't think so. Those addUnique* methods guarantee unique insertion into an array of properties. Nothing analogous exists that I know of for creation of unique instances.
Note that this won't guarantee uniqueness, but it will mitigate it. Is wiser to do this server-side, but even then uniqueness will not be guaranteed. Apparently there is no way to enforce absolute uniqueness for your own column at this time.
@Joey I was unaware that there is a limitation. Under what circumstances would this fail uniqueness?
Imagine the scenario where 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.