hashmap containskey 删除firebase上的所有匹配数据
hashmap containskey delete all mathcing data on firebase
我有一个按钮,它是一个计数器,如果“stars”子项不为真,则加 1,否则 -1。首先按下,执行下面的 2 个语句。实际上,我必须将我的 UID 保留在 userkey
child 中,但是随着第一次按下被删除,同时我的 starcount 正常添加,它必须转到 stars child 从那里删除,但这直接删除了我的 UID。
final String uid = currentUser.getUid();
DatabaseReference allfeed = db.getReference("allfeed").child(post_key);
allfeed.runTransaction(new Transaction.Handler() {
@NonNull
@Override
public Transaction.Result doTransaction(@NonNull MutableData currentData) {
ExampleItem star = currentData.getValue(ExampleItem.class);
if (star == null) {
return Transaction.success(currentData);
}
if (star.stars.containsKey(uid)) {
star.starCount = star.starCount - 1;
star.stars.remove(uid);
} else {
// Star the post and add self to stars
star.starCount = star.starCount + 1;
star.stars.put(uid, true);
}
currentData.setValue(star);
return Transaction.success(currentData);
}
@Override
public void onComplete(@Nullable DatabaseError error, boolean committed, @Nullable DataSnapshot currentData) {
}
});
public class ExampleItem {
public ExampleItem(){
}
String userid,key,reply,time,text,username;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<String, Boolean>();
//
// GETTER AND SETTER
//
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("keyuser", userid);
result.put("username", username);
result.put("text", text);
result.put("key", key);
return result;
}
}
当您尝试使用以下方法将 Firebase 实时数据库中的节点映射到 ExampleItem
类型的对象时:
ExampleItem star = currentData.getValue(ExampleItem.class);
表示class中的每个字段都会保存节点中存在的对应字段的值。这意味着 class 中的所有字段在数据库中都应该完全匹配。
由于 Firebase 实时数据库中的每个节点都可以被视为一个地图,当您使用 setValue() 方法时:
currentData.setValue(star);
这意味着您覆盖了该特定位置的数据。由于您的 ExampleItem
class 没有名为 userKey
的字段,该字段将从数据库中删除。因此,(突出显示的阅读)删除操作。要解决此问题,您必须将 class 中的字段从:
更改为
String userid,key,reply,time,text,username;
收件人:
String userid,userKey,reply,time,text,username;
这样,userKey
字段就不会再被删除了。
我有一个按钮,它是一个计数器,如果“stars”子项不为真,则加 1,否则 -1。首先按下,执行下面的 2 个语句。实际上,我必须将我的 UID 保留在 userkey
child 中,但是随着第一次按下被删除,同时我的 starcount 正常添加,它必须转到 stars child 从那里删除,但这直接删除了我的 UID。
final String uid = currentUser.getUid();
DatabaseReference allfeed = db.getReference("allfeed").child(post_key);
allfeed.runTransaction(new Transaction.Handler() {
@NonNull
@Override
public Transaction.Result doTransaction(@NonNull MutableData currentData) {
ExampleItem star = currentData.getValue(ExampleItem.class);
if (star == null) {
return Transaction.success(currentData);
}
if (star.stars.containsKey(uid)) {
star.starCount = star.starCount - 1;
star.stars.remove(uid);
} else {
// Star the post and add self to stars
star.starCount = star.starCount + 1;
star.stars.put(uid, true);
}
currentData.setValue(star);
return Transaction.success(currentData);
}
@Override
public void onComplete(@Nullable DatabaseError error, boolean committed, @Nullable DataSnapshot currentData) {
}
});
public class ExampleItem {
public ExampleItem(){
}
String userid,key,reply,time,text,username;
public int starCount = 0;
public Map<String, Boolean> stars = new HashMap<String, Boolean>();
//
// GETTER AND SETTER
//
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("keyuser", userid);
result.put("username", username);
result.put("text", text);
result.put("key", key);
return result;
}
}
当您尝试使用以下方法将 Firebase 实时数据库中的节点映射到 ExampleItem
类型的对象时:
ExampleItem star = currentData.getValue(ExampleItem.class);
表示class中的每个字段都会保存节点中存在的对应字段的值。这意味着 class 中的所有字段在数据库中都应该完全匹配。
由于 Firebase 实时数据库中的每个节点都可以被视为一个地图,当您使用 setValue() 方法时:
currentData.setValue(star);
这意味着您覆盖了该特定位置的数据。由于您的 ExampleItem
class 没有名为 userKey
的字段,该字段将从数据库中删除。因此,(突出显示的阅读)删除操作。要解决此问题,您必须将 class 中的字段从:
String userid,key,reply,time,text,username;
收件人:
String userid,userKey,reply,time,text,username;
这样,userKey
字段就不会再被删除了。