gpt4 book ai didi

ios - 将数百个 PFObject 保存到 Parse 云

转载 作者:行者123 更新时间:2023-11-29 02:23:26 27 4
gpt4 key购买 nike

我有一个应用程序,其中有两个解析表 - “用户”和“主题”。现在我想要的是,如果任何用户注册,那么我想创建另一个解析表,该表将存储与该用户和主题相关的一些信息,例如主题进度。这是我的代码 -

- (IBAction)signUpFunction {
[self.view endEditing:YES];
NSString *fullName = self.nameTextField.text;
NSString *username = self.usernameTextField.text;
NSString *password = self.passwordTextField.text;
NSString *email = self.emailTextField.text;
if ([username length] == 0 || [password length] == 0 || [email length] == 0 || [fullName length] == 0)
{
[[[UIAlertView alloc] initWithTitle:@"Missing Information"
message:@"Make sure you fill out all of the information!"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
}
else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.password = password;
newUser.email = email;
newUser[@"fullName"] = fullName;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {

[[[UIAlertView alloc] initWithTitle:@"Error!"message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show ];
}
else
{
PFQuery *topicsQuery = [PFQuery queryWithClassName:@"Topic"];
[topicsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {

for (unsigned int i = 0; i < objects.count; i++) {
PFObject *object = objects[i];
PFObject *topicProgressForUser = [PFObject objectWithClassName:@"QuizProgress"];
[topicProgressForUser setObject:[PFUser currentUser] forKey:@"user"];
[topicProgressForUser setObject:object forKey:@"topic"];

if ([object[@"fullAccess"] isEqualToString:@"Yes"]) {
[topicProgressForUser setObject:@"Free" forKey:@"purchased"];
} else {
[topicProgressForUser setObject:@"No" forKey:@"purchased"];
}
[topicProgressForUser setObject:0 forKey:@"questionsSolved"];
[topicProgressForUser setObject:0 forKey:@"attempts"];
[topicProgressForUser setObject:0 forKey:@"resultInPercentage"];
[topicProgressForUser setObject:@"Basic" forKey:@"achievement"];
[topicProgressForUser setObject:NO forKey:@"generateCertificate"];

[topicProgressForUser saveEventually];

}
}
}]; // topic block

}
}]; // signup block
}
}

我认为我没有使用正确的标准来保存数据来通过单独保存每个 pfobject 来解析云。如果在保存对象的过程中互联网连接丢失怎么办?任何人都可以帮助我使用正确且快速的方法将多个 pfobject 数据保存到解析云中的新表中。

最佳答案

对于您的情况,我会看一下类方法 saveAllsaveAllInBackground 等。

我采用了您的代码并按照我认为在这种情况下最有意义的方式对其进行了修改。让我知道它是否有效:

- (IBAction)signUpFunction {
[self.view endEditing:YES];
NSString *fullName = self.nameTextField.text;
NSString *username = self.usernameTextField.text;
NSString *password = self.passwordTextField.text;
NSString *email = self.emailTextField.text;
if ([username length] == 0 || [password length] == 0 || [email length] == 0 || [fullName length] == 0) {
[[[UIAlertView alloc] initWithTitle:@"Missing Information"
message:@"Make sure you fill out all of the information!"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil] show];
} else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.password = password;
newUser.email = email;
newUser[@"fullName"] = fullName;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:@"Error!"message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show ];
} else {
PFQuery *topicsQuery = [PFQuery queryWithClassName:@"Topic"];
[topicsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error != nil) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
} else {
NSMutableArray *topics = [NSMutableArray array];
for (unsigned int i = 0; i < objects.count; i++) {
PFObject *object = objects[i];
PFObject *topicProgressForUser = [PFObject objectWithClassName:@"QuizProgress"];
[topics addObject:topicProgressForUser];
topicProgressForUser[@"user"] = [PFUser currentUser];
topicProgressForUser[@"topic"] = object;
topicProgressForUser[@"questionSolved"] = @(NO);
topicProgressForUser[@"attempts"] = @(0);
topicProgressForUser[@"resultInPercentage"] = @(0);
topicProgressForUser[@"achievement"] = @"Basic";
topicProgressForUser[@"generateCertificate"] = @(NO);
if ([object[@"fullAccess"] isEqualToString:@"Yes"]) {
topicProgressForUser[@"purchased"] = @"Free";
} else {
topicProgressForUser[@"purchased"] = @"No";
}
}
[PFObject saveAllInBackground:objects block:^(BOOL succeeded, NSError *error) {
if (error != nil) {
// Do something here to handle the error

} else {
//
}
}
]; // saveAllInBackground
}
}]; // topic block
}
}]; // signup block
}
}

关于ios - 将数百个 PFObject 保存到 Parse 云,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838431/

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