如何使parse.com对象属性唯一?

Atm*_*tma 4 ios parse-platform

我具有PFUser需要唯一的对象的属性。

例如,默认电子邮件字段是唯一的,当不符合条件时会引发异常并带有警报。

如何将此字段设置为唯一?

dan*_*anh 5

为了澄清起见,解析会在User模型上强制执行电子邮件唯一性,但是您希望在其他字段(例如,employeeId等虚构的字段)上强制唯一性。

要在客户端进行此操作,请首先执行查询以确保满足条件:

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);
        }];
    }
}];
Run Code Online (Sandbox Code Playgroud)

要在服务器端执行此操作,可能有可能beforeSave在用户模型上执行基本相同的操作。

Parse.Cloud.beforeSave(Parse.User, function(request, response) {});
Run Code Online (Sandbox Code Playgroud)