Android 编辑包含多个回调的数组,并等待它们全部完成
我正在用在线数据库(Firestore)替换我的Sqlite数据库。为此,数据库的每个答案都通过回调返回给我 问题是,我在一个循环中对数据库进行了多次调用,该循环填充了一个表,而该表是不可访问的,除非我最终声明了它,因此我无法更改它 因此,我正在寻找一种在不完全修改现有代码的情况下填充此表的方法。我看到了Android 编辑包含多个回调的数组,并等待它们全部完成,android,google-cloud-firestore,Android,Google Cloud Firestore,我正在用在线数据库(Firestore)替换我的Sqlite数据库。为此,数据库的每个答案都通过回调返回给我 问题是,我在一个循环中对数据库进行了多次调用,该循环填充了一个表,而该表是不可访问的,除非我最终声明了它,因此我无法更改它 因此,我正在寻找一种在不完全修改现有代码的情况下填充此表的方法。我看到了ArrayBlockingQueue,但我想知道是否没有更简单的解决方案 如果可能的话,我希望将所有变量都保留在函数中,但我还没有找到解决方案 我知道对于这个例子,我们不一定需要一个表,但我想保
ArrayBlockingQueue
,但我想知道是否没有更简单的解决方案
如果可能的话,我希望将所有变量都保留在函数中,但我还没有找到解决方案
我知道对于这个例子,我们不一定需要一个表,但我想保留它,因为它只是一个例子;)
之前(SQLite)
游戏中的公共整数玩家(整数id玩家){
int games其中playerisher=0;
ArrayList gamesArray=数据库.getGamesArray();
用于(游戏:gamesArray)
if(Utils.isplayerpresentinggame(game.getId(),idPlayer))
游戏玩家界面++;
返回玩家所在的游戏;
}
之后(带回调)
private static int counter=0;
私有静态int resultNP=0;
私有静态ArrayBlockingQueue结果;
public static void numberGamesWherePlayerIsPresent(最终长idPlayer,最终Callbacks.IntCallback){
线程线程=新线程(){
@凌驾
公开募捐{
游戏(新回调。ListGameCallback(){
@凌驾
公开作废onCallback(ArrayList gameArrayList){
counterNumberGamesWherePlayerIsPresent=gameArrayList.size();
结果=新建ArrayBlockingQueue(gameArrayList.size());
for(游戏:gameArrayList){
Utils.isplayerpresentinggame(game.getId(),idPlayer,新回调。BooleanCallback()){
@凌驾
公共void onCallback(布尔布尔布尔){
如果(bool)
结果:增加(1);
其他的
结果:添加(0);
}
});
}
int结果;
试一试{
而(计数器>0){
result=results.take();
计数器--;
resultNP+=结果;
}
}捕获(中断异常ie){
即:fillInStackTrace();
Log.e(标记“results.take()failed”);
}
callback.onCallback(resultNP);
}
});
}
};
setName(“Firestore-numberGamesWherePlayerIsPresent()”;
thread.start();
}
与Firestore相关的代码在哪里?@AlexMamo所有接受回调参数的函数都是数据库调用,我不会将代码放在这里,因为它没有用处,但通常该函数只是一个Firestore.path.get.addOnCompleteListener()
public int player_in_x_game(int id_player) {
int gamesWherePlayerIsHere = 0;
ArrayList<Games> gamesArray = Database.getGamesArray();
for (Game game: gamesArray)
if(Utils.isPlayerPresentInGame(game.getId(), idPlayer))
gamesWherePlayerIsHere++;
return gamesWherePlayerIsHere;
}
private static int counter= 0;
private static int resultNP = 0;
private static ArrayBlockingQueue<Integer> results;
public static void numberGamesWherePlayerIsPresent(final long idPlayer, final Callbacks.IntCallback callback){
Thread thread = new Thread() {
@Override
public void run() {
games(new Callbacks.ListGameCallback() {
@Override
public void onCallback(ArrayList<Game> gameArrayList) {
counterNumberGamesWherePlayerIsPresent= gameArrayList.size();
results = new ArrayBlockingQueue<>(gameArrayList.size());
for (Game game: gameArrayList){
Utils.isPlayerPresentInGame(game.getId(), idPlayer, new Callbacks.BooleanCallback() {
@Override
public void onCallback(boolean bool) {
if (bool)
results.add(1);
else
results.add(0);
}
});
}
int result;
try {
while (counter > 0) {
result = results.take();
counter--;
resultNP += result;
}
}catch (InterruptedException ie){
ie.fillInStackTrace();
Log.e(TAG,"results.take() failed");
}
callback.onCallback(resultNP);
}
});
}
};
thread.setName("Firestore - numberGamesWherePlayerIsPresent()");
thread.start();
}