2014-09-28 29 views
0

我正在使用解析作爲我的應用程序的數據庫。 我在應用程序中創建了兩個對象之間的許多關係。取自解析文檔如何獲得關係的另一方

// 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對象? 謝謝

回答

1

這不是一個雙向關係。您將不會從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