![](/img/trans.png)
[英]All notifications for specific user are being deleted from database, when only one should be removed
[英]The player exits the game activity, and the room should be removed only by him, but the rooms of all players are deleted
我在做一個游戲,當玩家退出游戲活動時,他創建的房間被刪除了,問題是在“rooms/”路徑中,它的所有內容都被刪除了,而不僅僅是玩家的房間。 一般來說,除了他的房間,其他人以及如何修復它之外,玩家的葉子都會被移除。
這張照片的例子顯示了房間中有兩個房間,當玩家離開一個房間時,兩個房間被移除。
這是我正在使用的代碼:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Query applesQuery = ref.child("rooms").orderByChild(roomName);
applesQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) {
appleSnapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
當您創建查詢並將偵聽器附加到它時,您會得到一個包含所有數據的DataSnapshot
object。 當您遍歷結果並調用時:
appleSnapshot.getRef().removeValue();
這意味着您刪除了DataSnapshot
object 中的每個子項。 例如,如果您只想刪除Ben
,則無需查詢。 您可以簡單地創建一個指向 Ben 節點的引用並像這樣調用removeValue()
:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference benRef = db.child("rooms").child("Ben");
benRef.removeValue();
如果需要,您還可以將偵聽器附加到操作,以查看是否出現問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.