将数百个PFObject保存到Parse云中

时间:2015-01-08 10:57:37

标签: ios objective-c parse-platform pfquery pfobject

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

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

2 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

更改以下代码行:

[topicProgressForUser saveEventually];

[topicProgressForUser saveInBackground];

希望这会有所帮助..