如何避免不同的用户将同一个字符串保存到同一列中?如果已经创建了对象,则需要检查特定列...
我没有为PFUser类尝试这个,检查重复的用户,默认情况。
任何代码线索,启动示例?
下面是我保存对象的代码。字段_channelName是无法复制的字段,必须在保存之前进行检查。
- (IBAction)addButton:(id)sender {
// Create a new Post object and create relationship with PFUser
PFObject *newPost = [PFObject objectWithClassName:@"Channel"];
[newPost setObject:[_textView text] forKey:@"about"];
[newPost setObject:[_channelName text] forKey:@"name"];
[newPost setObject:[_enterKey text] forKey:@"enterkey"];
[newPost setObject:[PFUser currentUser] forKey:@"owner"]; // One-to-Many relationship created here!
NSData *imageData = UIImageJPEGRepresentation(self.channelImage.image, 0.8);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];
[newPost setObject:imageFile forKey:@"image"];
// Set ACL permissions for added security
PFACL *postACL = [PFACL ACL];
[postACL setPublicReadAccess:YES];
[PFACL setDefaultACL:postACL withAccessForCurrentUser:YES];
//PFACL *postACL = [PFACL ACLWithUser:[PFUser currentUser]];
[postACL setPublicReadAccess:YES];
[newPost setACL:postACL];
// Save new Post object in Parse
[newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self dismissViewControllerAnimated:YES completion:nil];
// Dismiss the viewController upon success
} else {
//implement upload error message
}
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
Thanx和欢呼!!!!
答案 0 :(得分:1)
您可以通过两种不同的方式实现此目的。您可以使用云代码,也可以在客户端执行此操作。
云代码看起来像这样:
Parse.Cloud.beforeSave("YourClass", function(request, response) {
var query = new Parse.Query("YourClass");
query.equalTo("uniqueColumn",request.object.get("uniqueColumn"));
query.first({
success: function(object) {
if (object){
var currentUser = request.object;
if (currentUser.id === object.id) { // if it already belongs to the user it's good
response.success();
} else { // otherwise it already exists and it belongs to someone else
response.error("uniqueColumn exists");
}
} else { // If the object doesn't exist at all it's available
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this uniqueColumn.");
}
});
});
您可以使用findObjectsInBackgroundWithBlock方法在客户端执行此操作,并检查返回的数组上的count是否大于0.这样看起来像这样:
PFQuery *query = [PFQuery ...];
[query whereKey:@"name" equalTo:[_channelName text]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects.count > 0) {
// Not unique, name already exist, handle it here
} else {
// Name is unique, proceed to save your object here
// Create a new Post object and create relationship with PFUser
PFObject *newPost = [PFObject objectWithClassName:@"Channel"];
[newPost setObject:[_textView text] forKey:@"about"];
[newPost setObject:[_channelName text] forKey:@"name"];
[newPost setObject:[_enterKey text] forKey:@"enterkey"];
[newPost setObject:[PFUser currentUser] forKey:@"owner"]; // One-to-Many relationship created here!
NSData *imageData = UIImageJPEGRepresentation(self.channelImage.image, 0.8);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];
[newPost setObject:imageFile forKey:@"image"];
// Set ACL permissions for added security
PFACL *postACL = [PFACL ACL];
[postACL setPublicReadAccess:YES];
[PFACL setDefaultACL:postACL withAccessForCurrentUser:YES];
//PFACL *postACL = [PFACL ACLWithUser:[PFUser currentUser]];
[postACL setPublicReadAccess:YES];
[newPost setACL:postACL];
[newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self dismissViewControllerAnimated:YES completion:nil];
// Dismiss the viewController upon success
} else {
//implement upload error message
}
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}];
//...