保存唯一键到 Firebase 的子节点

huangapple go评论74阅读模式
英文:

save unique key firebase to child

问题

以下是已翻译的内容:

我有一个像这样的数据库

"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
  "isSeen" : false,
  "message" : "H",
  "receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
  "sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
  "timestamp" : "1595571732585",
  "type" : "text"
}}

我想要将唯一键"-MCzU6r-JqvAkC9unjru"保存在我的子项中,就像这样

"Chats" : {
"-MCzU6r-JqvAkC9unjru" : {
  "isSeen" : false,
  "message" : "H",
  "receiver" : "wwRWdMQ62bNwO09M5LpxXks37442",
  "sender" : "R5ICmEp5B6Sbhow523BmQoF6uUG2",
  "timestamp" : "1595571732585",
  "type" : "text",
  "key" : "-MCzU6r-JqvAkC9unjru"
}}

我可以这样做吗?如果可以,我该如何在我的代码中实现它?

这里是向 Firebase 添加数据的代码

private void sendMessage(final String message) {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

    String timestamp = String.valueOf(System.currentTimeMillis());

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("sender", myUid); 
    hashMap.put("receiver", hisUid);
    hashMap.put("message", message);
    hashMap.put("timestamp", timestamp);
    hashMap.put("isSeen", false);
    hashMap.put("type", "text");
    databaseReference.child("Chats").push().setValue(hashMap);
}
英文:

I have a database like this

&quot;Chats&quot; : {
&quot;-MCzU6r-JqvAkC9unjru&quot; : {
  &quot;isSeen&quot; : false,
  &quot;message&quot; : &quot;H&quot;,
  &quot;receiver&quot; : &quot;wwRWdMQ62bNwO09M5LpxXks37442&quot;,
  &quot;sender&quot; : &quot;R5ICmEp5B6Sbhow523BmQoF6uUG2&quot;,
  &quot;timestamp&quot; : &quot;1595571732585&quot;,
  &quot;type&quot; : &quot;text&quot;
}}

I want to save a unique key "-MCzU6r-JqvAkC9unjru" in my child, like this

&quot;Chats&quot; : {
&quot;-MCzU6r-JqvAkC9unjru&quot; : {
  &quot;isSeen&quot; : false,
  &quot;message&quot; : &quot;H&quot;,
  &quot;receiver&quot; : &quot;wwRWdMQ62bNwO09M5LpxXks37442&quot;,
  &quot;sender&quot; : &quot;R5ICmEp5B6Sbhow523BmQoF6uUG2&quot;,
  &quot;timestamp&quot; : &quot;1595571732585&quot;,
  &quot;type&quot; : &quot;text&quot;,
  &quot;key&quot; : &quot;-MCzU6r-JqvAkC9unjru&quot;
}}

can I do that? if can how I can implement it in my code?

here code for add data to firebase

private void sendMessage(final String message) {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

    String timestamp = String.valueOf(System.currentTimeMillis());

    HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
    hashMap.put(&quot;sender&quot;, myUid); 
    hashMap.put(&quot;receiver&quot;, hisUid);
    hashMap.put(&quot;message&quot;, message);
    hashMap.put(&quot;timestamp&quot;, timestamp);
    hashMap.put(&quot;isSeen&quot;, false);
    hashMap.put(&quot;type&quot;, &quot;text&quot;);
    databaseReference.child(&quot;Chats&quot;).push().setValue(hashMap);

答案1

得分: 2

你可以按照以下方式操作:

    String key = databaseReference.child("Chats").push().getKey();

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("sender", myUid); 
    hashMap.put("receiver", hisUid);
    hashMap.put("message", message);
    hashMap.put("timestamp", timestamp);
    hashMap.put("isSeen", false);
    hashMap.put("type", "text");
    hashMap.put("key", key);
    databaseReference.child("Chats").child(key).setValue(hashMap);

使用 getKey() 可以获取使用 push() 创建的随机 ID,然后将它添加到 hashMap 中。

英文:

You can do the following:

    String key = databaseReference.child(&quot;Chats&quot;).push().getKey();

    HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
    hashMap.put(&quot;sender&quot;, myUid); 
    hashMap.put(&quot;receiver&quot;, hisUid);
    hashMap.put(&quot;message&quot;, message);
    hashMap.put(&quot;timestamp&quot;, timestamp);
    hashMap.put(&quot;isSeen&quot;, false);
    hashMap.put(&quot;type&quot;, &quot;text&quot;);
    hashMap.put(&quot;key&quot;,key);
    databaseReference.child(&quot;Chats&quot;).child(key).setValue(hashMap);

Using getKey() you can retrieve the random id created using push() and then add it to the hashMap

huangapple
  • 本文由 发表于 2020年7月24日 14:43:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定