Concurrent transactions in firebase leading to zero when reading dataSnapshot

Two applications use the same firebase database. One application, i.e. A writes some value using setValue . 2nd application, i.e. B tries to read some data using addListenerForSingleValueEvent , which has the value A , such as A . in the same time. This results in a datasnapshot value of zero in Appendix B when reading the database. Can anyone guide me, how can I avoid this situation.

Just for some cleaning up, A writes to some other node from the one that B is accessing .

Here is how I can access the data from the application Bed and .

DatabaseReference usersRef = MyDatabaseUtils.getUsersReference();
    usersRef.orderByChild("userId").equalTo(userIdValue).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get value
            if (dataSnapshot.getChildrenCount() > 0) {
                 // Do something
            } else {
                // Show error
            }
        }

        @Override
        public void onCancelled(final DatabaseError databaseError) {
           // Show error
        }
    });

Everything works fine, if I stop writing A in the reading time in the annex Bed and .

+4
source share
1 answer

The simplest solution would be to protect datasnapshotagainst zero in a function onDataChange:

if (dataSnapshot != null && dataSnapshot.getChildrenCount() > 0) 

Perhaps when A is written to Firebase, it removes some pre-existing attributes usersRef.

+2
source

Source: https://habr.com/ru/post/1680082/


All Articles