问题描述
我正在Android Studio中编写应用程序.它连接到Firebase Firestore.
I'm writing an app in Android Studio. It connects to Firebase Firestore.
数据的结构如下(为清楚起见,我删除了无关的位):
The data is structured like this (I've removed the irrelevant bits, for clarity):
Boxes
- box1
- grid_id
- table_id
-box2
- grid_id
- table_id
Grids
- grid1
- grid2
Tables
- table1
- table2
因此,每次我检索一个框时,我还需要检索相应的Grid和Table.
So, each time I retrieve a box, I also need to retrieve the corresponding Grid and Table.
在我的活动中,我有以下内容:
In my Activity, I have the following:
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不会引发任何错误,并且调试控制台没有任何错误.我怀疑这是因为我正在同时检索网格和表格-会减慢它的速度吗?如果是这样,有什么更好的方法呢?还是我错过了其他事情?
The problem I'm having is that the app seems to 'freeze' and then restart when I hit this activity. The screen goes blank and unresponsive, and then the app restarts. Android Studio isn't throwing any errors, and the debug console has nothing. I suspect it's because I'm retrieving the grid and table simultaneously - would that slow it down? If so, what's a better way to do it? Or have I missed something else?
推荐答案
要解决此问题,建议您在内添加
对象.这样,您将可以使用单个侦听器,而不是三个.因此,您的代码应如下所示: Grid
和 Table
对象框
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);
}
}
}
}
});
并删除 FragmentManager
和 BoxFragment
的声明.两者都应该只出现在 onComplete()
方法内部.
And remove the declaration of your FragmentManager
and BoxFragment
. Both should only be present inside the onComplete()
method.
这篇关于我可以同时从两个不同功能的数据库中检索两个文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!