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

Android orderByChild()equalTo()在某些情况下返回空值,android,firebase,firebase-realtime-database,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" : { "e

在底部更新。
我的数据结构如下:

"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等于用户的uid。因此,我想从users节点获取与传递给equalTo()方法的uid相同的子uid。应使用以下代码执行此操作:

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的警告)

问题在于,当我在一台设备上运行android应用程序时(用户asd@gmail.com)查询返回正确的数据(它找到具有给定uid的用户。但是当我在另一个设备(帐户)上尝试时asd2@gmail.com)事实并非如此——dataSnapshot.getValue()始终为null,并且代码不断向数据库添加同一用户(相同的uid)一遍又一遍。当我打印出来时,uid值实际上是相同的。 我做错了什么

更新 我注意到,删除以下行修复了该问题:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
但我真的不明白这是怎么回事,尤其是这两台设备一直在线

FirebaseDatabase.getInstance().setPersistenceEnabled(true);