【发布时间】:2018-03-27 11:16:03
【问题描述】:
我在FirebaseDatabase 中有一些数据,如下所示:
app
-child1
-uniqueId1
-pId1
-lId1
-uniqueId2
-pId2
-lId2
-uniqueId3
-pId3
-lId3
-uniqueId4
-pId4
-lId4
-uniqueId5
-pId5
-lId5
-uniqueId6
-pId6
-lId6
-child2
-uniqueIdA1
-uniqueId7
-uniqueId8
-uniqueId9
-uniqueId10
-uniqueId11
-uniqueId1
-uniqueId2
-uniqueId3
-uniqueId4
-uniqueId5
我正在检索child1 的数据,如下所示:
public void fMethod(final String fID, final String blackListedId) {
mDatabase.child("child1").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
Profile profile = dataSnapshot.getValue(Profile.class);
String pID = profile.getPID();
String lID = profile.getLID();
if (!pID.trim().equals(AccessToken.getCurrentAccessToken().getUserId().trim())) {
if (pID.trim().equals(fID.trim())) {
if (!lID.trim().equals(blackListedId.trim())) {
// populate the view with elements which meet this condition/requirement
String listingID = profile.getlID();
Log.d("LISTING_IDS", listingID);
} else {
Log.d("dataSnapshot", "null1");
}
} else {
Log.d("dataSnapshot", "null2");
}
} else {
Log.d("dataSnapshot", "null3");
}
} else {
Log.d("dataSnapshot", "null4");
}
}
...
...
...
}
和child2的数据如下:
public void fData(final String fID) {
mDatabase.child("child2").child(AccessToken.getCurrentAccessToken().getUserId()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String blackListedId = childSnapshot.getValue().toString();
fMethod(fID, blackListedId);
}
} else {
fMethod(fID, "");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
然后我在另一个代码中检索fIDs 并在那里调用fData() 方法。
我记录了从数据库中获取的所有 ID:
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId1
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId2
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId3
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId4
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
D/LISTING_IDS: uniqueId5
这是Profile.java 文件的代码:https://gist.github.com/HammadNasir/a196bcdc6dccbf69657fca528443e680
问题是在fMethod()的if语句中条件是!lID.trim().equals(blackListedId.trim()所以,正如你在数据库中看到的,我应该得到child1下的所有uniqueIds,除了uniqueId3和@987654337 @ 因为这 2 个也出现在 child2 中,但我得到了所有的 uniqueIds,除了 uniqueId3 和 uniqueId7 两次和 uniqueId3 和 uniqueId7 一次。
要注意的另一件事是,当我将条件设为lID.trim().equals(blackListedId.trim() 时,我只会得到符合此要求的 2 个 ID,即 uniqueId3 和 uniqueId7,如果 child2 只有uniqueId11 下的 1 个 id 然后我会得到所有的 uniqueIds,除了这里的那个但有 2 个或更多 id 会导致问题。
我希望你能解决我的问题。我尽量用最少的代码来解释。
为什么!lID.trim().equals(blackListedId.trim() 会返回意外的 id,我怎样才能只获取满足此条件的 id?
【问题讨论】:
-
@Ibrahim 你不熟悉 Firebase 吗?
-
好吧,我认为您的案例不依赖于 firebase 技能,它更多地依赖于调试技能,尝试调试值并检查您的条件,我相信这是最快的解决方案。
-
记录您从服务器获取的所有内容。然后用代码发布你的 logcat。
-
我在您的代码中看不到任何日志命令
-
记录所有 fID、黑名单 ID、AccessToken。然后比较它们。你会发现哪里出了问题。目前,您只是记录结果
标签: android firebase if-statement firebase-realtime-database