使用 Firebase 中的 push() 键删除第一个 child
Deleting the first child using a push() key in Firebase
我目前正在开发一款严格使用 Firebase 作为后端的应用程序。在我的应用程序中,我想删除 30 条消息后写的第一条消息。换句话说,我只想允许存储 30 条消息。在我的 firebase 数据库中,我有一个名为 "users" 的节点,然后是一个用于用户 ID 的节点,然后在其中一个用户 ID 下,我有另一个名为 items 的节点。但是,我想删除项目节点下的第一个 child,但我不知道 child 的名称,因为我正在使用 .push() 方法为其设置键.有人知道如何只删除第一个 child 吗?
我知道如何在 Firebase 中删除 child,我只是不知道如何访问那个 child。我尝试了以下方法:
DatabaseReference firstMessage = mDatabase.child("users").child(mUserId).child("items").getRef();
然后执行此操作以删除 child。
firstMessage.removeValue();
我知道在 .child("items") 之后应该有一个额外的 child 但我如何获得那个密钥?
编辑:我想出了一个解决方案,但不是删除项目节点中的第一个 child,而是删除用户节点中的第一个 child
DatabaseReference firstMessage = mDatabase.child("users").child(mUserId).child("items").orderByPriority().limitToFirst(1).getRef();
您可以在您的 onDataChange 中尝试这个,
for (DataSnapshot p : dataSnapshot.getChildren()) {
String childKey = p.getKey();
}
你可以通过这个得到所有child的所有密钥。
您可以将它们添加到 arraylist 中或执行其他操作。
如果你想删除一些child,
使用
myRef.child("item").child("childKey_0").setValue(null);
==================================
2017 年 3 月 4 日编辑:
我无法回答你的新问题。只是一些使用 arrayList 的例子。
ArrayList<String> childKey_list = new ArrayList<>();
for (DataSnapshot p : dataSnapshot.getChildren()) {
childKey_list.clear(); //clear the arrayList everytime the firebase refresh
String childKey = p.getKey();
childKey_list.add(childKey);
}
If (some_method_of_time) {
String delete_childID = childKey_list.get(0);
myRef.child("item").child(delete_childID).setValue(null);
}
对于iOS - Swift 4
self.ref.queryOrderedByPriority().queryLimited(toFirst: 1).observe(.value, with:{ snapshot in
guard let key = snapshot.value // as? [String:Any]
else {
print("Error")
return
}
print("key:",key)
//key has the first child of a node
})
我目前正在开发一款严格使用 Firebase 作为后端的应用程序。在我的应用程序中,我想删除 30 条消息后写的第一条消息。换句话说,我只想允许存储 30 条消息。在我的 firebase 数据库中,我有一个名为 "users" 的节点,然后是一个用于用户 ID 的节点,然后在其中一个用户 ID 下,我有另一个名为 items 的节点。但是,我想删除项目节点下的第一个 child,但我不知道 child 的名称,因为我正在使用 .push() 方法为其设置键.有人知道如何只删除第一个 child 吗?
我知道如何在 Firebase 中删除 child,我只是不知道如何访问那个 child。我尝试了以下方法:
DatabaseReference firstMessage = mDatabase.child("users").child(mUserId).child("items").getRef();
然后执行此操作以删除 child。
firstMessage.removeValue();
我知道在 .child("items") 之后应该有一个额外的 child 但我如何获得那个密钥?
编辑:我想出了一个解决方案,但不是删除项目节点中的第一个 child,而是删除用户节点中的第一个 child
DatabaseReference firstMessage = mDatabase.child("users").child(mUserId).child("items").orderByPriority().limitToFirst(1).getRef();
您可以在您的 onDataChange 中尝试这个,
for (DataSnapshot p : dataSnapshot.getChildren()) {
String childKey = p.getKey();
}
你可以通过这个得到所有child的所有密钥。 您可以将它们添加到 arraylist 中或执行其他操作。
如果你想删除一些child, 使用
myRef.child("item").child("childKey_0").setValue(null);
==================================
2017 年 3 月 4 日编辑:
我无法回答你的新问题。只是一些使用 arrayList 的例子。
ArrayList<String> childKey_list = new ArrayList<>();
for (DataSnapshot p : dataSnapshot.getChildren()) {
childKey_list.clear(); //clear the arrayList everytime the firebase refresh
String childKey = p.getKey();
childKey_list.add(childKey);
}
If (some_method_of_time) {
String delete_childID = childKey_list.get(0);
myRef.child("item").child(delete_childID).setValue(null);
}
对于iOS - Swift 4
self.ref.queryOrderedByPriority().queryLimited(toFirst: 1).observe(.value, with:{ snapshot in
guard let key = snapshot.value // as? [String:Any]
else {
print("Error")
return
}
print("key:",key)
//key has the first child of a node
})