如何使用PFQuery作为NSDictionary的值存储的PFObject数组

我在iOS应用程序中使用parse.com将数据存储到parsing云服务。 我遇到了嵌套对象查询的麻烦。 我有以下数据模型:

“游戏”类包含“赢家”

“winners”是一个NSDictionary的数组, NSDictionary中的每一项都是一个Player到N Powers的映射

playerPowers的值是PFObject的一个数组(PFObject) (当前只有一个名字),用于key:objectId (PFObject)

对于每个获胜者,我添加到“赢家”(可以有多个获奖者)一个NSDictionary对象,如下所示:

 NSDictionary * winnerWithPowers = [NSDictionary dictionaryWithObject:tempPowers forKey:[aWinnerPFObject objectId]]; [newGame addObject:winnerWithPowers forKey:@"winners"]; 

对于字典中的每个项目,密钥是玩家的现有对象PFObjects ,值也是服务器上的PFObjects (权力)数组。 当我查询“优胜者”时,我想检索所有数据填充,所有获奖者和他们各自的权力PFObjects与他们的所有数据。 当我查询“赢家”每个functionPFObject的细节是不完整的(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]; } }]; 

/ /在tableview 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 ); } 

当您检索与其他PFObjects(一系列权力)相关的PFObject (游戏)时,这些权力的值不会被检索。 您将不得不在后续的获取请求中获取这些权力的所有值。

从parsing文档:

默认情况下,在获取对象时,不会获取相关的PFObjects。 这些对象的值不能被检索,直到他们被提取像这样:

 PFObject *post = [fetchedComment objectForKey:@"parent"]; [post fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) { NSString *title = [post objectForKey:@"title"]; }]; 

澄清对提取与查找:在PFObjects( 文档 )上调用提取,而在PFQueries( 文档 )中使用查找。

提取需要PFObjects作为input,并且不返回任何东西。 提取只是更新您已经从Parse中检索的PFObjects的值。 另一方面,查找将从Parse中检索PFObjects。

既然你有一个Powers数组(PFObjects),使用下面的代码从Parse中检索所有的值:

 [PFObject fetchAllIfNeeded:(NSArray *)myPowers]; 

fetchAllIfNeededInBackground:如果你想它是asynchronous的。

 PFQuery *query = [PFQuery queryWithClassName:@"Cars"]; [query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) { for (PFObject *comment in comments) { PFObject *post = [comment objectForKey:@"name"]; NSLog(@"%@",post);