【发布时间】:2021-06-11 21:53:56
【问题描述】:
我正在尝试实现一个 executorService 以在后台删除一个巨大的 Firebase 节点。该节点每十秒获取一条新记录,以提供实时线性图形(259K 记录/月)。用户需要定时清理数据的功能,需要手动触发。
我已经编写了以下方法:
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(() -> {
Log.i(TAG, "cleanTable: execute");
tabelasRef = fbDB.child(tableName).child(dispositivoSel.getIdDispositivo());
tabelasEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
try{
Log.i(TAG, "onDataChange: childrencount " + snapshot.getChildrenCount());
if (snapshot.getChildrenCount() > 0) {
// Inicializa a ProgressBar
new Handler(Looper.getMainLooper()).post(() -> {
progressBarLimpaTabela.setMax((int) snapshot.getChildrenCount());
progressBarLimpaTabela.setVisibility(View.VISIBLE);
Log.i(TAG, "Tornando a ProgressBar visível e definindo max=" + snapshot.getChildrenCount());
});
progress = 1;
for (DataSnapshot ds : snapshot.getChildren()) {
// Apaga registro
fbDB.child(tableName).child(dispositivoSel.getIdDispositivo()).child(ds.getKey()).removeValue();
// Atualiza a ProgressBar
new Handler(Looper.getMainLooper()).post(() -> {
progressBarLimpaTabela.setProgress(progress);
Log.i(TAG, "Atualizando progress: " + progress);
});
progress ++;
}
// Finaliza a ProgressBar
new Handler(Looper.getMainLooper()).post(() -> {
progressBarLimpaTabela.setVisibility(View.INVISIBLE);
Log.i(TAG, "fechando ProgressBar.");
});
}
}catch (Exception ex){
new Handler(Looper.getMainLooper()).post(() ->
logMsg(AdminActivity.this, tableName + "-EventListener.onDataChange(Exception): " + ex.getMessage(), true, true, false, false)
);
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
logMsg(AdminActivity.this, tableName + "-EventListener.onCancelled(DatabaseError): " + error.getMessage(), true, true, false, false);
}
});
}
};
tabelasRef.addListenerForSingleValueEvent(tabelasEventListener);
});
executorService.shutdown();
问题是进度条永远不会显示或更新。我可以在 logcat 中看到显示日志需要一段时间,当它显示时是这样的:
2021-06-11 18:29:17.833 14051-14272/projectname I/LOGCAT: cleanTable: execute
2021-06-11 18:29:29.394 14051-14051/projectname I/LOGCAT: onDataChange: childrencount 105606
2021-06-11 18:30:42.405 14051-14051/projectname I/LOGCAT: Tornando a ProgressBar visível de definindo max=105606
2021-06-11 18:30:42.406 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.526 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.526 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.531 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.531 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.607 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.607 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.851 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:42.851 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
...
...
...
2021-06-11 18:30:48.473 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:49.433 14051-14051/projectname I/LOGCAT: Atualizando progress: 105607
2021-06-11 18:30:49.433 14051-14051/projectname I/LOGCAT: fechando ProgressBar.
请帮我找出我的错误在哪里。谢谢!
【问题讨论】:
标签: java android progress-bar executorservice