如何将PFQuery用于存储为NSDictionary值的PFObject数组

Bej*_*max 1 nsdictionary ios parse-platform

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

"游戏"类包含"获胜者"

"获胜者"是一个数组NSDictionary,字典中的每个项目都是1个玩家到N个人的映射

playerPowers值是一个PFObjects数组(当前只有一个名称的权力)key:objectId of a player(PFObject)

对于每个获胜者,我向"赢家"(可能有多个获胜者)添加一个NSDictionary对象,如下所示:

NSDictionary * winnerWithPowers = [NSDictionary dictionaryWithObject:tempPowers
                                                forKey:[aWinnerPFObject objectId]];
[newGame addObject:winnerWithPowers forKey:@"winners"];
Run Code Online (Sandbox Code Playgroud)

对于字典中的每个项目,键是播放器的现有objectId,值PFObjects也是服务器上的(幂)数组.当我查询"获胜者"时,我想要检索所有填充的数据,所有获胜者及其各自的权力PFObjects及其所有数据.当我查询"获胜者"时,每个权力的细节PFObject都是不完整的(键的值:名称为空).以下是查询,然后是打印结果的代码,然后输出包含两个获胜者的字典:

//在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];
    }
}];
Run Code Online (Sandbox Code Playgroud)

//在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]);
}
Run Code Online (Sandbox Code Playgroud)

日志:

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
);
}
Run Code Online (Sandbox Code Playgroud)

Bio*_*Bio 8

当您检索PFObject与其他PFObject(一组权力)相关的(游戏)时,不会检索这些权力的值.您必须在后续的提取请求中获取这些Powers的所有值.

从Parse文档:

默认情况下,在获取对象时,不会获取相关的PFObject.在获取这些对象的值之前,无法检索这些对象的值:

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

关于Fetch vs Find的澄清:在PFObjects(docs)上调用Fetches,而在PFQueries(docs)中使用Finds .

Fetches需要PFObjects作为输入,并且不返回任何内容.Fetches只是更新你已经从Parse中检索到的PFObjects上的值.另一方面,Finds将从Parse中检索PFObject.

由于您有一个Powers数组(它们是PFObject),请使用以下命令从Parse中检索所有值:

[PFObject fetchAllIfNeeded:(NSArray *)myPowers];
Run Code Online (Sandbox Code Playgroud)

或者fetchAllIfNeededInBackground:如果你想要它是异步的.