我试图这样做,以便PFUser
或client
创建PFObject
时,他们无法创建/再创建一个。{/ p>
这里我展示了如何创建PFObject
:
-(IBAction)New:(id)sender
{
PFObject *New = [PFObject objectWithClassName:@"client"];
PFUser *user = [PFUser currentUser];
[New setObject:user forKey:@"user"];
[New setObject:[[self Name] text] forKey:@"name"];
[New setObject:[[self Team] text] forKey:@"team"];
[New setObject:[[self Points] text] forKey:@"points"];
[New setObject:[[self Height] text] forKey:@"height"];
[New setObject:[[self Weight] text] forKey:@"weight"];
[New setObject:[[self Position] text] forKey:@"position"];
[New setObject:[[self Game] text] forKey:@"game"];
[New setObject:[[self Hometown] text] forKey:@"hometown"];
[New setObject:[[self Birthday] text] forKey:@"birthday"];
// [New setObject:[[self Bio] text] forKey:@"bio"];
NSData *imageData = UIImageJPEGRepresentation(PlayerImage.image, 0.8);
NSString *filename = [NSString stringWithFormat:@"%@.png", Name.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[New setObject:imageFile forKey:@"imageFile"];
[New saveInBackground];
NSLog(@"Object Added");
}
使用此代码,PFObject
已添加到PFUser
,现在我需要一种方法来使User
无法创建另一个PFObject
答案 0 :(得分:2)
您应该使用云功能实现此功能。
在您的云端功能中,您可以检查用户是否已设置client
,并执行更新而不是添加,或只返回错误。
示例云代码,iOS中无需更改,只是阻止保存“客户端”(如果当前用户已有):
// returns an error if a client is already set on the user
Parse.Cloud.beforeSave("client", function (request, response) {
var user = request.user;
user.fetch().then(function (user) {
if (user.get("client")) {
response.error("user already has a client record");
return;
} else {
response.success();
return;
}
}, function (error) {
response.error(error);
});
});
// links the client to the user if the save was allowed
Parse.Cloud.afterSave("client", function (request, response) {
var user = request.user;
user.set("client", request.object);
user.save().then(function () {
response.success();
},
function (error) {
response.error(error);
});
});