[英]Use Parse.com and Android, how can I get a list of Games that a pair of Players share?
I am making a two-player for Android using Parse.com. 我正在使用Parse.com为Android制作两人游戏。 I'm determining which two players are in the game by introducing a
Game
table on Parse.com, with an Array
of the userId
s of the players
of the game. 我通过在Parse.com上引入
Game
表以及游戏players
的userId
Array
来确定游戏中有两个玩家。
I need to query Parse.com for a list of all the games shared by two given players. 我需要查询Parse.com,以获得两个给定玩家共享的所有游戏的列表。 I thought something like this would do the trick.
我以为这样的事情可以解决。
String[] userIds = new String[] {player1, player2};
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Game");
query.whereEqualTo("players", userIds);
The problem is, I don't know the order of the Array of players. 问题是,我不知道玩家数组的顺序 。 So about half of the time, the order will be wrong I won't get the correct result from this query.
因此,大约有一半的时间,顺序是错误的,我无法从该查询中获得正确的结果。 I'll get that no game exists between the two players.
我会知道两个玩家之间没有游戏。
So my idea to solve this was to also query for a reversed array, then OR the two queries together. 因此,我解决此问题的想法是还查询一个反向数组,然后将两个查询或在一起。
String[] reversedUserIds = new String[] {player2, player1};
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("Game");
query2.whereEqualTo("players", reverseUserIds);
List<ParseQuery<ParseObject>> queryList = new ArrayList<ParseQuery<ParseObject>>();
queryList.add(query);
queryList.add(query2);
ParseQuery<ParseObject> mainQuery = ParseQuery.or(queryList);
However, when I run mainQuery.find()
, I get an error that looks like this: 但是,当我运行
mainQuery.find()
,出现如下错误:
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Ljava.lang.String;
Instead of simply returning a list of all the games that both players are playing with each other. 而不是简单地返回两个玩家都在玩的所有游戏的列表。
You could try to force the IDs pair to be always ordered, for example doing player1.compareTo(player2)
. 您可以尝试强制将ID对始终排序,例如,执行
player1.compareTo(player2)
。 Then you only need to query with the right order. 然后,您只需要以正确的顺序进行查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.