【发布时间】:2019-04-05 08:31:40
【问题描述】:
我正在使用这种方法将数据存储在我的 firebase 数据库中:
首先,我将所有数据存储在一个名为 SubjectDataModel 的模型类中, 然后我从 firebase 数据库中获取推送键。
然后我为该特定键设置值。
这是我的代码:
主题数据模型:
public class SubjectDataModel {
public String id;
public String dbName;
public String subName;
public String tagline;
public int preference;
public SubjectDataModel()
{
}
public SubjectDataModel(String id, String dbName, String subName, String tagline, int preference) {
this.id = id;
this.dbName = dbName;
this.subName = subName;
this.tagline = tagline;
this.preference = preference;
}
}
然后我使用以下代码将其推送到数据库,然后我还将密钥 id 存储在本地。
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Data");
String id = ref.push().getKey();
SubjectDataModel newSub = new SubjectDataModel(id, txt_dbName, txt_subName, txt_tagline, txt_preference);
ref.child(id).setValue(newSub);
现在想象一下,稍后我想更新这些数据,
所以我存储了密钥 ID,所以我可以访问它,我还在本地编辑了所有其他数据,所以现在如果我用该数据创建一个 SubjectDataModel 对象并再次使用存储的 ID 执行 ref.child(id).setValue(newSub);,数据会是更新 ?还是有其他方法可以做到这一点?
【问题讨论】:
标签: android firebase firebase-realtime-database