android - Completion handler or equivalent in Android
问题描述
So I have a question in regards to getting data from Firebase Realtime DB for Android, as it seems that completion handlers are not possible as is in iOS.
I would like to gather a list of users and their status in a particular group they are part of. My Firebase DB is structured as such:
UserInGroup
--- GroupID
--- UserId : true/false (true indicates user is in the group)
Users
--- UserId
--- Username : String
--- ...
GroupStatus
--- GroupId
--- UserId: true/false (true indicates has admin rights)
Based on the above I would like to have a Table where it will list the user's "username" followed by the status of either admin or normal user in the group.
My code I have so far to perform the first portion of the lookup is below:
userInGroupReference = mFirebaseDatabase.getReference("UserInGroup");
userInGroupQuery = userInGroupReference.child("10263").orderByValue().equalTo(true);
userInGroupQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot resultSnapshot : dataSnapshot.getChildren()) {
// THIS IS WHERE I AM GETTING THE USERS ID (KEY) TO THE OTHER TWO LOOKUPS I NEED TO PERFORM
String userID = resultSnapshot.getKey();
... What do I do now with no completion handlers!?
}
} else {
// dataSnapshot doesn't exist
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I am just confused on how to utilize the userID I get from the first lookup and perform the additional two lookups using the userID in android!?
I mean if it was iOS I would just use completion handlers to accomplish this... how can I do so in Android?
UPDATE:
I created the interface:
private interface FirebaseCallback {
void onSuccess(DataSnapshot dataSnapshot);
void onStart();
void onFailure();
}
Then created the readData function
private void readData(Query query, FirebaseCallback listener) {
listener.onStart();
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
listener.onSuccess(dataSnapshot);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
And placed the readData function in my OnCreate function in the Activity as such:
readData(userInGroupQuery, new FirebaseCallback() {
@Override
public void onSuccess(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String userId = snapshot.getKey();
// Get userId data from database....now you can use the retrieved data
getUsername(userId);
getUserStatus(userId);
}
}
@Override
public void onStart() {
// When starting
Log.d("ONSTART", "Started");
}
@Override
public void onFailure() {
// If failed
Log.d("onFailure", "Failed");
}
});
So now my question is when I call the following two (2) functions:
// Get userId data from database....now you can use the retrieved data
getUsername(userId);
getUserStatus(userId);
how do I get the values for for each iteration I send over for the second lookup in Firebase?
2ND UPDATE:
public void getUsername(String userId) {
userReference = mFirebaseDatabase.getReference("Users");
userQuery = userReference.child(userId).child("username");
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String username = dataSnapshot.getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and the last lookup:
private void getUserStatus(String userId) {
statusQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
Boolean status = (Boolean) dataSnapshot.getValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
My goal is to have the values of the two look-ups populate a List
List<UserResults> userList;
public class UserResults {
private String username;
private boolean status;
public UserResults(String username, boolean status) {
this.username = username;
this.status = status;
}
}
Which is the part I am stuck on now.
解决方案
推荐阅读
- c# - Microsoft.Graph - 如何从具有不同用户名的共享邮箱发送?
- php - 我在 Drupal 8 中使用标志模块面临问题(csrf_token' URL 查询参数无效)
- asp.net-core - System.TypeLoadException:'无法从程序集'WindowsBase,版本 = 4.0.0.0 加载类型'System.Windows.Size'
- python - 如何将 HTTP Basic Auth 用作单独的 FastAPI 服务?
- flutter - 在单个类中管理多个 setState() 方法?
- python - cli命令的顺序执行
- python - 如何创建一个数据框,它将在 Python 中给出多个数据框的摘要?
- c++ - 第二个对话框上的 C++ MFC 按钮什么也不做
- jenkins - 使用声明性管道将 Jenkins 变量注入 Maven
- powershell - 指定函数返回类型