【问题标题】:How does Parse.com create a bidirectional relationship in the iOS client?Parse.com 如何在 iOS 客户端中创建双向关系?
【发布时间】:2014-05-19 14:10:53
【问题描述】:
【问题讨论】:
标签:
ios
parse-platform
relationship
【解决方案1】:
Parse 不是纯粹的关系数据库,这不是自然的方式。
通常是在子表中定义一个指针类型,指向父表。
// 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 访问的自然方式。
但是如果你需要创建一个双向关系,你可以这样做。
https://parse.com/questions/bidirectional-relationship-one-to-many
【解决方案2】:
您可以将用户设置为帖子的post_owner,并查询以当前用户为所有者的帖子