我在Android应用程序中使用Firebase realtime database。我在表中的条目大约为300。我无法从Firebase中的Datasnapshot获取数据。有时,它在加载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", "");
        }
    });
}

最佳答案

为了提高Firebase查询的性能,您可以在订购查询的字段上添加索引。喜欢

  {
   "rules": {
      "YOUR_NODE": {
         ".indexOn": ["FIELD1", "FIELD2"]
     }
   }
  }


您可以找到帮助链接here

Firebase还说here


重要提示:每次数据被调用时,onDataChange()方法都会被调用
在指定的数据库引用处已更改,包括对
孩子们。要限制快照的大小,请仅在
观看更改所需的最高级别。例如,附加一个
不建议您监听数据库的根。


但是,即使仍未应用该索引,也要花费20分钟的时间,但您仍需要检查源代码,UI中可能存在其他问题,UI中可能有数据但没有填充。

07-24 16:55
查看更多