我从data-1表中获取用户列表,如下面的代码所示。
DatabaseReference rootReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference reference = rootReference.child("data-1")
ValueEventListener listener= new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final ArrayList<String> userList= new ArrayList<>();
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String foundUserId= dSnapshot.getKey();
if (condition) {
userList.add(foundUserId);
//step one completed: userList is full with all necessary users
//Now how to iterate it and compare it with data from table-2?
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
reference.addListenerForSingleValueEvent(listener);
}
到目前为止,userList
拥有data-1
表中找到的所有用户。但是,我仍然需要过滤掉一些用户,并将它们添加到名为filteredUserList
的第二个列表中。我需要通过将每个用户与保存在final
变量currentUserId
我从data-1
获取用户列表,其中每个用户都有一个用户列表,如下所示:
data-1
|_____uid1
| |_____uid3: true
| |_____uid4: true
|
|_____uid25
|_____uid34: true
|_____uid101: true
您在data-1
中看到每个用户下方都有一个用户列表,这就是我首先填写userList
的方式。
table-2
具有时间戳节点,这是它的结构:
table-2
|
|____userid1
| |
| |_____timestamp: timestamp
|
|____userid2
|
|_____timestamp: timestamp
//and so on..
我想做的是,在userList
被用户填满之后,我需要检查每个用户的timestamp
是否在当前用户的特定时间增量内。因此,我需要迭代userList
,并将每个用户的timestamp
与当前用户的timestamp
进行比较。但是我无法使其工作,因为为了获得每个用户的时间戳,我需要一个新的onDataChange
,并且在for循环中,但是它不在范围内,并且也是异步的,因此它永远无法工作!使它起作用的正确方法是什么?我已经尝试了一个多星期。甚至有可能做到这一点?
答案 0 :(得分:1)
我能想到的最简单的解决方案是获取当前用户timestamp
,然后查询整个users
节点,并将当前用户timestamp
与其他用户时间戳进行比较。
我需要通过将每个用户与当前用户进行比较来过滤掉他们
为解决此问题,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users")
DatabaseReference uidRef = usersRef.child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long currentUserTimestamp = dataSnapshot.child("timestamp").getValue(Long.class);
String currentUserKey = dataSnapshot.getKey();
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
long timestamp = dataSnapshot.child("timestamp").getValue(Long.class);
if(currentUserKey != ds.getKey()) {
if(currentUserTimestamp > timestamp) {
//Do something
} else {
//Do something else
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
usersRef.addListenerForSingleValueEvent(eventListener);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
编辑:根据评论和已编辑的问题,请参见以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference table2Ref = rootRef.child("table-2");
DatabaseReference uidRef = table2Ref.child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long currentUserTimestamp = dataSnapshot.child("timestamp").getValue(Long.class);
String currentUserKey = dataSnapshot.getKey();
DatabaseReference currentUserKeyRef = rootRef.child("data1").child(currentUserKey);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String userKey = ds.getKey();
DatabaseReference userKeyRef = table2Ref.child(userKey);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot d : dataSnapshot.getChildren()) {
long timestamp = d.child("timestamp").getValue(Long.class);
if(currentUserTimestamp > timestamp) {
//Do something
} else {
//Do something else
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
userKeyRef.addListenerForSingleValueEvent(listener);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
currentUserKeyRef.addListenerForSingleValueEvent(eventListener);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);