【问题标题】:Why do I get 2 sets of children when i use setAsyncValue?为什么我使用 setAsyncValue 时会得到 2 组孩子?
【发布时间】:2019-06-04 20:59:32
【问题描述】:

我不确定为什么在使用 setValueAsync 时会创建/更新 2 个孩子。也许有人可以帮助我理解我做错了什么。

最初,我在 Employee 节点下手动设置了一些子节点(姓名和密码)。现在我正在尝试创建一个进程,以便能够从 java 中的应用程序更新它们。由于某些未知原因,创建/更新了 2 组子项(appPassword、name、password、technicianName)。即使我删除了任何集合,它们也会在我执行 setValueAsync 时自动添加。

这是我的课:

package com.mycompany.spring.model;

public class TechShort {
    public boolean active;
    public String name;
    public String password;

public TechShort(boolean active, String name, String password) {
    this.active;
    this.name = name;
    this.password = password;
}

public boolean getActive() {return this.active;}
public void setActive(boolean active) {this.active = active;} 
public String getTechnicianName() {return this.name;}
public void setTechnicianName(String name) {this.name = name;}     
public String getAppPassword() {return this.password;}
public void setAppPassword(String password) {this.password = password;}     
}

这就是我加载 firebase 数据库的方式:

Map<String, TechShort> techs;
techs = new HashMap<>();
techs.put((String.valueOf(i+1)),
       new  TechShort(Boolean.valueOf(model.getValueAt(i,6).toString()),
                                name1,password1));

 FirebaseDatabase firedb = FirebaseDatabase.getInstance();
                    DatabaseReference db = firedb.getReference().child("Employees");
                    db.setValueAsync(techs);

所以一切正常,孩子们被创建,但他们是重复的,我希望节点下只有 3 个孩子( 例子 活跃:真 密码:1123223 姓名:乔治

相反,我有: 活跃:真 应用密码:1123223 姓名:乔治 密码:1123223 技术员姓名:乔治

如果我将其中一个值(例如 1123223 更改为其他值,appPassword 和密码都会更新。希望有人可以帮助/指导我了解如何解决此问题。

【问题讨论】:

  • 您共享的代码中没有任何内容可以解释数据库中的值重复。因此,这似乎更有可能发生在您调用共享代码的方式中。
  • 我正在循环加载地图并使用 put 添加元素(如在 techs.put(...) 中),然后使用 db.setValueAsync(techs);没有别的,这就是为什么它让我感到困惑。我将尝试以编程方式创建节点,然后使用 setValueAsync 设置它,看看会怎样。

标签: java firebase-realtime-database firebase-admin


【解决方案1】:

只要我从以下位置修改了 getter/setter:

public boolean getActive() {return this.active;}
public void setActive(boolean active) {this.active = active;} 
public String getTechnicianName() {return this.name;}
public void setTechnicianName(String name) {this.name = name;}     
public String getAppPassword() {return this.password;}
public void setAppPassword(String password) {this.password = password;}

public boolean getActive() {return this.active;}
public void setActive(boolean active) {this.active = active;} 
public String getName() {return this.name;}
public void setName(String name) {this.name = name;}     
public String getPassword() {return this.password;}
public void setPassword(String password) {this.password = password;}  

重复消失了。似乎 getter/setter 和变量应该匹配,否则会有重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多