Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

One to many parse

I would like to create a one to many object but it doesn't seem to be working. Here is my code below.

- (IBAction)Send {

    // Create a new Post object and create relationship with PFUser
    PFObject *newPost = [PFObject objectWithClassName:@"Post"];
    [newPost setObject:[self.textField text] forKey:@"textContent"];
    [newPost setObject:[PFUser currentUser] forKey:@"author"]; // One-to-Many relationship created here!

    //Set ACL permissions for added security
    PFACL *postACL = [PFACL ACLWithUser:[PFUser currentUser]];
    [postACL setPublicReadAccess:YES];
    [newPost setACL:postACL];

    // Save new Post object in Parse
    [newPost saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            //[self dismissModalViewControllerAnimated:YES]; // Dismiss the viewController upon success
            NSLog(@"Error");
        }
    }];
}

3 Answers

Alessandro Covre
Alessandro Covre
7,057 Points

What do you get from this code? Are you sure about the one-to-many relationship? Because you create a new post and you set an author for that post, this is a one-to-one relationship, for an author you can have multiple posts, that is the one-to-many, am I wrong? :)

I got the code from the parse one to many tutorial. What should I be doing differently? I am trying to do this for a messaging app if that helps.

Alessandro Covre
Alessandro Covre
7,057 Points

The code seems to be correct if every author can have many posts. Exactly, what is the result you want to obtain?

I would like it to take the text from a textfield and upload to parse. However it does not seem to be uploading.

Alessandro Covre
Alessandro Covre
7,057 Points

Did you get an error somewhere? Since the code is correct, the only thing that I can image is that you are not logged with a user. If you are not logged in, [PFUser currentUser] returns NULL and then you get an exception.