如何从firebase dataSnapshot获取值的计数?

时间:2018-02-14 08:41:43

标签: android firebase firebase-realtime-database

这是我的OpenActivity.class我从firebase实时数据库中读取数据,我想得到特定值的计数。

我的实时数据库节点如下所示,

-Notificationsss
   -ID
     -message1: "Hello"
     -notificationStatus: "Opened"
     -type1: "asada"



-Notificationsss
       -ID
         -message1: "Hello"
         -notificationStatus: "UnOpened"
         -type1: "asada"

我希望获得价值计数"未开启"

 mFirebaseDatabase = FirebaseDatabase.getInstance();
        mDatabaseReference = mFirebaseDatabase.getReference("Notificationssss");

        notificationList = new ArrayList<>();   

     mDatabaseReference.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            try {


      notificationList.clear();



        for (DataSnapshot notificationSnapshot : dataSnapshot.getChildren()) {
                            notification = notificationSnapshot.getValue(Notification.class);
                            notificationList.add(notification);

                        }

                        NotificationList adapter = new NotificationList(OpenNotification.this,notificationList);
                        mListView.setAdapter(adapter);

                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

1 个答案:

答案 0 :(得分:1)

Firebase不存储元数据或对属性执行的读/写次数的计数。要实现这一点,您需要通过添加计数器来实现此逻辑,每次从数据库读取或写入数据时都必须更新该计数器。

如果您认为此计数器可以由并发用户更新,我强烈建议您使用Firebase Transactions