我正在Android Studio中编写应用程序。它连接到Firebase Firestore。
数据结构如下(为清楚起见,我删除了无关的位):
Boxes
- box1
- grid_id
- table_id
-box2
- grid_id
- table_id
Grids
- grid1
- grid2
Tables
- table1
- table2
因此,每次检索一个框时,我还需要检索相应的网格和表格。
在我的活动中,我有以下内容:
public class BoxViewActivity {
String box_id;
Box box;
FirebaseFirestore databaseRef;
BoxFragment boxFragment;
GridFragment gridFragment;
TableFragment tableFragment;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_box);
fragmentManager = getSupportFragmentManager();
boxFragment = fragmentManager.findFragmentById(R.id.fragment_box);
// Retrieve Box from database and set it up
Intent intent = getIntent();
box_id = intent.getStringExtra("BoxId");
box = new Box();
databaseRef = FirebaseFirestore.getInstance();
boxDocRef = databaseRef.collection("boxes").document(box_id);
boxDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
// Set up Box
box = document.toObject(Box.Class);
if (box != null) {
// Set up some box views
}
// Get grid/table key
setUpGrid(box.getGridId());
setUpTable(box.getTableId());
}
}
}
});
}
private void setUpGrid(String gridId) {
DocumentReference gridRef = databaseRef.collection("grids").document(gridId);
gridRef.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
// Retrieve Grid
Grid grid= document.toObject(Grid);
if (grid != null) {
boxFragment.setGrid(grid);
}
}
}
}
});
}
private void setUpTable(String tableId) {
if(tableId == null) return;
DocumentReference tableRef = databaseRef.collection("tables").document(tableId);
tableRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Table table = document.toObject(Table.Class);
if (table != null) {
boxFragment.setTable(table);
}
}
}
});
}
}
我遇到的问题是应用程序似乎“冻结”然后在我点击此活动时重新启动。屏幕变为空白且无响应,然后应用程序重新启动。 Android Studio没有抛出任何错误,调试控制台什么都没有。我怀疑是因为我正在同时检索网格和表格 - 这会减慢它的速度吗?如果是这样,有什么更好的方法呢?或者我错过了其他什么?
答案 0 :(得分:1)
To solve this, I suggest you add a Grid
and a Table
object within a Box
object. In this way you'll be able to a use a single listener instead of three. So your code should look like this:
boxDocRef = databaseRef.collection("boxes").document(box_id);
boxDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
FragmentManager fragmentManager = getSupportFragmentManager();
BoxFragment boxFragment = fragmentManager.findFragmentById(R.id.fragment_box);
if (document.exists()) {
// Set up Box
box = document.toObject(Box.Class);
if (box != null) {
// Set up some box views
// Get grid/table key
Grid grid = box.getGrid();
boxFragment.setGrid(grid);
Table table = box.getTable();
boxFragment.setTable(table);
}
}
}
}
});
And remove the declaration of your FragmentManager
and BoxFragment
. Both should only be present inside the onComplete()
method.