在Parse.com中检索PFQuery中嵌套数组的更好方法?

时间:2013-01-23 14:03:03

标签: objective-c-blocks nested-loops parse-platform

我正在使用Parse.com来存储iOS应用的数据。下面的代码成功检索属于PFObject“游戏”的嵌套数组中的所有值。但是,如果我需要查询另一个数组(与“赢家”相同的级别(比如说“输家”),我无法让它工作,并且数组输家中的所有值都不会被填充。我想我可以做到这些所有在主线程上,而不是尝试嵌套提取(嵌套块),但我想知道是否:

1)我存储数据的方式是否禁止我正确使用Parse的内置查询/获取功能?数据存储为:

PFObject * newGame = [PFObject objectWithClassName:@"Game"];
NSArray * winner = [NSArray arrayWithObjects:[_allPlayersPFDictionary objectForKey:[playerData objectAtIndex:0]], [playerData objectAtIndex:1], nil];
[_gamePF addObject:winner forKey:@"winners"];

2)是否有更好,更清晰的方法来进行查询并获取查询中所有嵌套数据的所有值?同样,获胜者不是PFObject,而是2个不同类型的PFObject数组的数组([PFObject fetchAll:(NSArray *)winnersArray]不起作用,因为数组中的所有对象必须与PFObject的“类型”相同)。我以这种方式存储它,因为每个获胜的玩家都有另一个PFObject(1对多)与它们相关的“权力”。

这是有效的查询,但我无法弄清楚如何向其添加“输家”并正确填充后台的所有数据。

PFQuery * gamesQuery = [PFQuery queryWithClassName:@"Game"];
[gamesQuery orderByDescending:@"createdAt"];
gamesQuery.limit = 30;
[gamesQuery findObjectsInBackgroundWithBlock:^(NSArray * theGames, NSError * error) {
    if (error) {
        NSLog(@"ERROR: There was an error with the Query to get Games!");
    } else {
        for (PFObject * aGame in theGames) {
            for (NSArray * aWinner in [aGame objectForKey:@"winners"]) {
                [[aWinner objectAtIndex:0] fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
                    if (error) {
                        NSLog(@"ERROR: There was an error with the Query to get Player in winnersArray!");
                    } else {
                        [PFObject fetchAllIfNeededInBackground:[aWinner objectAtIndex:1] block:^(NSArray *objects, NSError *error) {
                            if (error) {
                                NSLog(@"ERROR: There was an error with the Query to get Powers in winnersArray!");
                            } else {
                             [_gamesPF addObject:aGame];
                                NSLog(@"Games from viewDidLoad %@", _gamesPF);
                                [_tableView reloadData];
                            }
                        }];
                    }
                }];
            }
        }
    }
}];

1 个答案:

答案 0 :(得分:3)

嗯......我觉得有点傻。对于数据模型,以面向对象的方式使用Parse肯定要容易得多。能够通过重新构建数据轻松解决它:

游戏(PFObject *)有:

--> winners { (PFObject *), (PFObject *), ..., nil }
--> losers  { (PFObject *), (PFObject *), ..., nil }

将获胜者创建为:

[testWinner1 addObject:power1 forKey:@"power"];
[testWinner1 addObject:power2 forKey:@"power"];
[testWinner1 addObject:[_playerPFDictionary objectForKey:@"Tom"] forKey:@"player"];

然后使查询更容易,并且只涉及一个背景块:

PFQuery * gameQuery = [PFQuery queryWithClassName:@"TestGame"];
[gameQuery includeKey:@"winners.player"];
[gameQuery includeKey:@"winners.power"];
[gameQuery includeKey:@"losers.player"];
[gameQuery includeKey:@"losers.power"];
[gameQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"failed");
    } else {
        NSLog(@"testGame: %@", [objects objectAtIndex:0]);
    }
}];