orderByChild()equalTo()在某些情况下返回null值

时间:2017-04-06 17:44:56

标签: android firebase firebase-realtime-database

在底部更新。
我有这样的数据结构:

"users" : {
  "-KXc0PDja_SUJndi9_Zt" : {
    "email" : "asd@gmail.com",
    "imageUri" : "https://scontent.xx.fbcdn....jpg",
    "name" : "John Doe",
    "uid" : "FtJyW7OdZBYSvrFCPMBqNkeXup32"
  },
  "-Kh3Agg9n0HnfKH7r1v6" : {
    "email" : "asd2@gmail.com",
    "imageUri" : "https://lh3.googleusercontent.com/...photo.jpg",
    "name" : "John Doe",
    "uid" : "1H9EukKqzsazD6AQ10yEwS9ETHH3"
  }
}

我试图通过尝试获取数据来确定用户的数据是否已存储在我的数据库中,该数据的存储 uid 等于用户& #39; s uid 。因此,从 users 节点,我想获得具有相同 uid 的子节点,就像传递给 equalTo()方法的节点一样。以下代码应该这样做:

firebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(task -> {
                if (!task.isSuccessful()) {
                    Toast.makeText(activity, "Signing task not successful", Toast.LENGTH_SHORT).show();
                } else {
                    final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
                    final String uid = firebaseUser.getUid();
                    if (firebaseUser != null) {
                        usersRef.orderByChild(FirebaseConstants.CHILD_USERS_UID/*value: uid*/).equalTo(uid).addListenerForSingleValueEvent(
                                new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        if (!dataSnapshot.hasChildren()) { /*already tried dataSnapshot.getValue() == null*/
                                            /*In the cases explained in the description, this value is always null, even though the user with this exact uid already exists in the "users" node*/
                                            // create new entry in DB for this user
                                            final UserPojo userPojo =
                                                    new UserPojo(uid, firebaseUser.getDisplayName(),
                                                            firebaseUser.getPhotoUrl().toString(), firebaseUser.getEmail());
                                            final DatabaseReference usersBlankRef = usersRef.push();
                                            final String key = usersBlankRef.getKey();
                                            usersBlankRef.setValue(userPojo, (databaseError, databaseReference) -> {
                                                if (databaseError == null) {
                                                    final User user = User.fromPojo(userPojo);
                                                    user.setKey(key);
                                                    singleSubscriber.onSuccess(user);
                                                } else {
                                                    singleSubscriber.onError(new Throwable(databaseError.getDetails()));
                                                }
                                            });
                                        } else {
                                            // There should be only one user with the same UID.
                                            final DataSnapshot childSnapshot = dataSnapshot.getChildren().iterator().next();
                                            final UserPojo userPojo = childSnapshot.getValue(UserPojo.class);
                                            final User user = User.fromPojo(userPojo);
                                            user.setKey(childSnapshot.getKey());
                                            singleSubscriber.onSuccess(user);
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {
                                        singleSubscriber.onError(new Throwable(databaseError.getDetails()));
                                    }
                                });
                    }
                }
            })

我还添加了以下规则(在检查了Logcat日志之后,我发现了一个关于用户uid缺少.indexOn的警告)

{  
    "rules": {
        ".read": "auth != null",
        ".write": "auth != null",

        "users": {
            ".indexOn": ["uid"] 
        }
    }
}

问题是当我在一台设备(用户asd@gmail.com)上运行Android应用时,查询会返回正确的数据(它会找到具有给定uid的用户)。但是当我在另一台设备(帐号asd2@gmail.com)上尝试它时,它不会 - dataSnapshot.getValue()始终为null,并且代码不断向DB添加同一个用户(同一个uid)当我打印出来时,uid值实际上是相同的。 我究竟做错了什么?

更新 我观察到删除以下行修复了问题:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

但我真的不明白这是如何运作的,尤其是两台设备一直在线。

0 个答案:

没有答案