Parse.com如何在iOS客户端中创建双向关系? [英] How does Parse.com create a bidirectional relationship in the iOS client?

查看:46
本文介绍了Parse.com如何在iOS客户端中创建双向关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本教程: https://parse.com/tutorials/one-to-many-relationships

I'm following this tutorial: https://parse.com/tutorials/one-to-many-relationships

我看到已创建一个帖子,并且附加了当前用户.但是我看不到相反,也看不到Post如何附加到用户. 最后,它说: 应用程序现在应该能够创建Post对象,设置Posts和PFUser之间的一对多关系,以及使用查询来获取与给定用户关联的所有Posts." 那怎么可能?如果使用该代码,如果我从未将帖子附加到其用户,该如何检索属于该用户的所有帖子? 预先感谢!

I see a Post is created and the current user attached. But I don't see the opposite, I don't see how the Post is attached to the user. 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." 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!

推荐答案

解析不是纯关系数据库,这不是自然的方式.

Parse is not a pure relational database, this is not the natural way to do it.

通常是在子表中定义一个指向父表的指针类型.

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
    }
}];

没有通过子选择或联接进行SQL访问的自然方法.

There is not this natural way of SQL access through subselects or Join.

但是,如果您需要创建双向关系,则可以这样做.

But if you need to create a bidirectional relationship, you can do it by doing this.

https://parse.com/questions/bidirectional-relationship-one-一对多

这篇关于Parse.com如何在iOS客户端中创建双向关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆