Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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.