gpt4 book ai didi

ios - 只允许一个 PFUser 有一个 PFObject

转载 作者:行者123 更新时间:2023-11-28 22:03:06 25 4
gpt4 key购买 nike

我试图做到这一点,一旦 PFUserclient 创建了一个 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 Functions 来实现它。

在您的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com