![](/img/trans.png)
[英]PFQuery to load objects stored in an array in a PFObject into a PFQueryTableViewController
[英]How to use PFQuery for array of PFObject stored as values of NSDictionary
我在iOS應用中使用parse.com將數據存儲到解析雲服務。 我在查詢嵌套對象時遇到了麻煩。 我有以下數據模型:
“游戲”類別包含“獲勝者”
“優勝者”是NSDictionary
的數組, NSDictionary
中的每個項目都是1個玩家到N個強國的映射
playerPowers值是播放器(PFObject)的key:objectId的PFObjects數組 (當前僅具有功率)。
對於每個獲勝者,我向NSDictionary
對象添加“獲勝者”(可以有多個獲勝者),如下所示:
NSDictionary * winnerWithPowers = [NSDictionary dictionaryWithObject:tempPowers
forKey:[aWinnerPFObject objectId]];
[newGame addObject:winnerWithPowers forKey:@"winners"];
對於字典中的每個項目,鍵都是播放器的現有objectId,值也是服務器上的PFObjects
( PFObjects
)數組。 當我查詢“獲勝者”時,我想檢索所有填充的數據,所有獲勝者及其各自的力量PFObjects
及其所有數據。 當我查詢“優勝者”時,每個電源PFObject
的詳細信息PFObject
完整(key:name的值為null)。 以下是查詢,然后是打印結果的代碼,然后輸出包含兩個優勝者的字典:
//在viewWillAppear中:
PFQuery * gamesQuery = [PFQuery queryWithClassName:@"Game"];
[gamesQuery orderByDescending:@"createdAt"];
[gamesQuery findObjectsInBackgroundWithBlock:^(NSArray * theGames, NSError * error) {
if (error) {
NSLog(@"ERROR: There was an error with the Query for Games!");
} else {
_gamesPF = [[NSMutableArray alloc] initWithArray:theGames];
[_tableView reloadData];
}
}];
//在表格視圖中的cellForRowAtIndexPath:方法中(這是我自己的TableViewController)
NSArray * testArray = [[_gamesPF objectAtIndex:row] objectForKey:@"winners"];
if ([testArray count] > 0) {
// print contents of first dictionary winners entry
NSLog(@"TestDictfromPF %@", [testArray objectAtIndex:0]);
}
日志:
2013-01-18 09:42:26.430 GamesTourney[20972:19d03] TestDictfromPF {
jchtlqsuIY = (
"<Power:OlJfby1iuz:(null)> {\n}", // problem is {\n}. Data exists on server but not in local structure after query
"<Power:eQkMUThGOh:(null)> {\n}" // ditto
);
}
當您檢索與其他PFObject(冪數組)相關的PFObject
(游戲)時,不會檢索這些冪的值。 您將必須在后續的提取請求中提取這些Power的所有值。
從解析文檔中:
默認情況下,在獲取對象時,不會獲取相關的PFObject。 在像這樣獲取它們之前,無法檢索這些對象的值:
PFObject *post = [fetchedComment objectForKey:@"parent"];
[post fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
NSString *title = [post objectForKey:@"title"];
}];
關於訪存與查找的說明:訪存在PFObjects( docs )上進行,而查找在PFQueries( docs )中使用。
提取需要PFObjects作為輸入,並且不返回任何內容。 提取僅更新您已經從Parse中檢索到的PFObject上的值。 另一方面,查找將從解析中檢索PFObject。
由於您具有Powers數組(即PFObjects),因此可以使用以下命令從Parse中檢索所有值:
[PFObject fetchAllIfNeeded:(NSArray *)myPowers];
或fetchAllIfNeededInBackground:
如果您希望它是異步的。
PFQuery *query = [PFQuery queryWithClassName:@"Cars"];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
for (PFObject *comment in comments)
{
PFObject *post = [comment objectForKey:@"name"];
NSLog(@"%@",post);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.