2013-01-23 33 views
0

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

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,而是两种不同类型的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]; 
          } 
         }]; 
        } 
       }]; 
      } 
     } 
    } 
}]; 

回答

3

嗯......我觉得有点愚蠢。使用面向对象的方式解析数据模型非常容易。能够轻松地通过重塑数据要解决这个问题:

游戏(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]); 
    } 
}];