addListenerForSingleValueEvent的意外行为

时间:2016-10-08 14:11:40

标签: android firebase firebase-realtime-database

我试图简单地确定用户是否存在我们的firebase数据库中的给定ID。 该数据库具有以下简单的结构和内容:

{
  "users" : {
    "-KTYqHtVJ4Q4LtDl0uTv" : {
      "userID" : "0200000262",
      "storeName" : "Stapples hhbjngg",
      "userName" : "ris983"
    }
}

为此,我在给定节点的数据库上添加了单值事件的监听器。

问题是这个监听器在添加用户后无法正确检测到用户我认为这是因为我没有正确理解该行为。

使情况更加混乱的原因是,如果我添加一个完全独立于第一个侦听器的其他ChildEventListener,则第一个侦听器运行正常。

首先,dataSnapshot值为::

DataSnapshot { key = users, value = null }

如果启用了第二个侦听器,则datasnapshot正确包含内容:

DataSnapshot { key = users, value = {-KTZ78jMYR0CiYiNHVvo={userID=0200000262, userName=ris983, storeName=Stapples hhbjngg}} }

这里我传递了源代码。 提前谢谢。

 private  void retrieveFirebaseUserByID(final String userID, final FireBaseUserListener fireBaseUserListener) {
        final DatabaseReference usersDatabase = FirebaseDatabase.getInstance().getReference().child("users");
        usersDatabase.orderByChild("userID").equalTo(userID).addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            Iterator<DataSnapshot> iter = dataSnapshot.getChildren().iterator();
                            while (iter.hasNext()) {
                                DataSnapshot storeDataSnapshot = iter.next();

                                String firebaseUserKey = storeDataSnapshot.getKey();
                                FirebaseUser firebaseUser = storeDataSnapshot.getValue(FirebaseUser.class);
                                fireBaseUserListener.userFound(firebaseUser);
                            }
                        } else {
                            fireBaseUserListener.userNotFound();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        fireBaseUserListener.userListenerError();
                    }
                }
        );

        //Seems that without this listener(childeventlistener) the previous listener(eventlistener) is not working correctly but it results in not found even if the node with the given userID is existent.
         usersDatabase.orderByChild("userID").equalTo(userID).addChildEventListener(new ChildEventListener(){

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
                usersDatabase.removeEventListener(this);
                String userID = dataSnapshot.getKey();
                FirebaseUser firebaseUser = dataSnapshot.getValue(FirebaseUser.class);
                firebaseUser.setFirebaseID(userID);
                fireBaseUserListener.userFound(firebaseUser);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {}

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {}

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {}

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });


    }

更新

启用firebase DEBUG日志后,问题似乎来自firebase的持久性功能。 似乎即使连接可用,firebase也决定使用nul值触发valueevent。由于此查询是先前完成的。

这似乎从日志中非常清楚:

Persistence: Starting transaction.
Persistence: Saved new tracked query in 1ms
Persistence: Loaded 0 tracked queries keys for tracked queries [256] in 0ms
Persistence: Loaded a total of 2 rows for a total of 6 nodes at /users34 in 1ms (Query: 0ms, Loading: 1ms, Serializing: 0ms)
Persistence: Set 0 tracked query keys for tracked query 256 in 0ms
PersistentConnection: pc_0 - Listening on users34 (params: {i=sapID, sp=0200000262, ep=0200000262})
PersistentConnection: pc_0 - Adding listen query: users34 (params: {i=sapID, sp=0200000262, ep=0200000262})
Connection: conn_1 - Sending data: {d={a=q, r=7, b={q={i=sapID, sp=0200000262, ep=0200000262}, h=, p=users34, t=3}}, t=d}
WebSocket: ws_1 - Reset keepAlive. Remaining: 30438
Persistence: Transaction completed. Elapsed: 8ms
EventRaiser: Raising 1 event(s)
EventRaiser: Raising /users34: VALUE: null
FirebaseUtils.retrieveFirebaseUserBySapID.addListenerForSingleValueEvent.onDataChange: DataSnapshot { key = users34, value = null }

所以仍然不清楚为什么firebase返回持久化节点,即使连接可用,也不清楚如何避免这种情况或未来启用持久性的可能错误,需要创建一个额外的监听器对于第一个工作似乎更像是黑客而不是解决方案。

0 个答案:

没有答案