我试图这样做,以便一旦PFUser
或client
创建了PFObject
后,它们将无法再次创建/创建一个。
在这里,我展示了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
最佳答案
您应该使用Cloud Function来实现。
Cloud Functions
Calling Cloud Functions in iOS
在您的Cloud Function中,您可以检查用户是否已经设置了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);
});
});
关于ios - 允许PFUser仅一个PFObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24643553/