0

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

- (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 数据保存到解析云中的新表中。

4

2 回答 2

1

在你的情况下,我会看看类方法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
    }
}
于 2015-01-08T13:23:46.547 回答
0

更改以下代码行:

[topicProgressForUser saveEventually];

[topicProgressForUser saveInBackground];

希望这可以帮助..

于 2015-01-08T12:57:19.700 回答