简体   繁体   English

Parse.com如何在iOS客户端中创建双向关系?

[英]How does Parse.com create a bidirectional relationship in the iOS client?

I'm following this tutorial: https://parse.com/tutorials/one-to-many-relationships 我正在学习本教程: https//parse.com/tutorials/one-to-many-relationships

I see a Post is created and the current user attached. 我看到Post已创建并且当前用户已连接。 But I don't see the opposite, I don't see how the Post is attached to the user. 但我没有看到相反的情况,我没有看到Post如何附加到用户身上。 And at the end it says: "The application should now be able to create Post objects, set a one-to-many relationship between Posts and PFUsers, as well as use a query to obtain all Posts associated with a given user." 最后它说:“应用程序现在应该能够创建Post对象,在Posts和PFUsers之间设置一对多关系,以及使用查询来获取与给定用户关联的所有帖子。” How is that possible? 怎么可能? If I use that code, how can I retrieve all Posts that belong to a User if I never attached the Post to its User? 如果我使用该代码,如果我从未将帖子附加到其用户,如何检索属于用户的所有帖子? Thanks in advance! 提前致谢!

您可以将用户设置为帖子的post_owner,并查询以当前用户为所有者的帖子

Parse is not a pure relational database, this is not the natural way to do it. Parse不是纯粹的关系数据库,这不是自然的方式。

The usual is define a pointer type in your child table, pointing out to the father table. 通常是在子表中定义指针类型,指向父表。

    // Create a new Post object and create relationship with PFUser
PFObject *newPost = [PFObject objectWithClassName:@"Post"];
[newPost setObject:[textView 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 dismissViewControllerAnimated:YES completion:nil]; // Dismiss the viewController upon success
    }
}];

There is not this natural way of SQL access through subselects or Join. 通过子选择或Join没有这种自然的SQL访问方式。

But if you need to create a bidirectional relationship, you can do it by doing this. 但是,如果您需要创建双向关系,则可以通过执行此操作来实现。

https://parse.com/questions/bidirectional-relationship-one-to-many https://parse.com/questions/bidirectional-relationship-one-to-many

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM