如何获得关系的另一方

问题描述:

我正在使用解析作为我的应用程序的数据库。 我在应用程序中创建了两个对象之间的许多关系。取自解析文档如何获得关系的另一方

// Create the post 
PFObject *myPost = [PFObject objectWithClassName:@"Post"]; 
myPost[@"title"] = @"I'm Hungry"; 
myPost[@"content"] = @"Where should we go for lunch?"; 

// Create the comment 
PFObject *myComment = [PFObject objectWithClassName:@"Comment"]; 
myComment[@"content"] = @"Let's do Sushirrito."; 

// Add a relation between the Post and Comment 
myComment[@"parent"] = myPost; 

// This will save both myPost and myComment 
[myComment saveInBackground]; 

保存关系后,我将如何从myPost对象获取myComment对象? 谢谢

这不是一个双向关系。您将不会从myPost对象获取myComment对象。您将通过查询Comment类的myComment对象来查找其“父级”设置为myPost的注释。

PFQuery *query = [PFQuery queryWithClassName:@"Comment"]; 
[query whereKey:@"parent" equalTo:myPost]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    ... 
}]; 
+0

谢谢...我不知道为什么这没有流入我的头:) – YuviGr 2014-09-28 22:20:44