我在我的安卓应用中使用了Firebase realtime database。我在表中的条目大约是300个。我无法从Datasnapshot中的Firebase获取数据。有时它在加载20分钟后就能正常工作。如何快速访问我的数据和响应?对于相同的数据库和相同的查询,它在iOS中运行得很好,速度非常快。
private void checkBookingInfo() throws Exception {
mDatabaseReference.child(FireBaseConstants.BOOKING_INFO_TABLE).limitToFirst(10).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
countArrival = 0;
countDeparture = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//check arrivals date matches with today's date then increment counter
if (snapshot.hasChild(FireBaseConstants.ARRIVAL_DATE)) {
String currentDate = Utils.getCurrentDate();
String arrivalDate = snapshot.child(FireBaseConstants.ARRIVAL_DATE).getValue().toString();
String status = snapshot.child(FireBaseConstants.STATUS).getValue().toString();
if (currentDate.equalsIgnoreCase(arrivalDate) && !status.equalsIgnoreCase("Cancel")) {
countArrival++;
}
}
//check departure date matches with today's date then increment counter
if (snapshot.hasChild(FireBaseConstants.DEPARTURE_DATE)) {
String currentDate = Utils.getCurrentDate();
String departureDate = snapshot.child(FireBaseConstants.DEPARTURE_DATE).getValue().toString();
String status = snapshot.child(FireBaseConstants.STATUS).getValue().toString();
if (currentDate.equalsIgnoreCase(departureDate) && !status.equalsIgnoreCase("Cancel")) {
countDeparture++;
}
}
}
setValueInEditText();
} else {
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("notme", "");
}
});
}发布于 2019-05-28 13:31:01
若要提高Firebase查询的性能,可以在排序查询所依据的字段上添加索引。喜欢
{
"rules": {
"YOUR_NODE": {
".indexOn": ["FIELD1", "FIELD2"]
}
}
}您可以找到帮助链接here
Firebase还说here
要点:每次在指定的数据库引用处更改数据时都会调用onDataChange()方法,包括对子项的更改。要限制快照的大小,请仅在查看更改所需的最高级别进行连接。例如,不建议将侦听器附加到数据库的根目录。
但是,即使没有应用索引,仍然需要20分钟的时间,你需要检查你的源代码,在UI中可能有一些其他问题,可能是数据即将到来,但没有填充到UI中。
https://stackoverflow.com/questions/56335472
复制相似问题