向Firestore文档添加两个哈希映射

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

Adding 2 Hashmaps to a single document in Firestore

问题

我已经将一个Hashmap上传到了Firestore。当我尝试上传另一个Hashmap到同一个文档时,它会替换掉之前的Hashmap。为什么会这样呢?

我每次都更改String标题,但它仍然会替换掉之前的Hashmap。

String poll1 = Poll1.getText().toString().trim();
String poll2 = Poll2.getText().toString().trim();
String poll3 = Poll3.getText().toString().trim();
String title = Title.getText().toString().trim();

DocumentReference documentReference = firestore.collection("Polls").document("abc1");

Map<String, Object> nestedData = new HashMap<>();
nestedData.put(poll1, 0);
nestedData.put(poll2, 0);
nestedData.put(poll3, 0);

Map<String, Object> upload = new HashMap<>();
upload.put(title, nestedData);

documentReference.set(upload).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        Toast.makeText(polls.this, "已上传", Toast.LENGTH_LONG).show();
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Toast.makeText(polls.this, "上传失败", Toast.LENGTH_LONG).show();
    }
});
英文:

I have uploaded 1 Hashmap to firestore.And when i try to upload one more Hashmap to the same document it replaces my previous Hashmap.Why is it so?

I change the String title everytime and still it replaces the previous Hashmap.

        String poll1 = Poll1.getText().toString().trim();
        String poll2 = Poll2.getText().toString().trim();
        String poll3 = Poll3.getText().toString().trim();
        String title = Title.getText().toString().trim();



        DocumentReference documentReference = firestore.collection(&quot;Polls&quot;).document(&quot;abc1&quot;);

        Map&lt;String, Object&gt; nestedData = new HashMap&lt;&gt;();
        nestedData.put(poll1 , 0 );
        nestedData.put(poll2 , 0);
        nestedData.put(poll3 , 0);

        Map&lt;String, Object&gt; upload = new HashMap&lt;&gt;();
        upload.put(title, nestedData);

        documentReference.set(upload).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
            @Override
            public void onComplete(@NonNull Task&lt;Void&gt; task) {
                Toast.makeText(polls.this, &quot;UPLOADED&quot;, Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(polls.this, &quot;FAILED&quot;, Toast.LENGTH_LONG).show();
            }
        });

答案1

得分: 1

你的文档需要提到,这必须合并而不是替换。

.set(data, SetOptions.merge())

如果有效,尝试这样做。

documentReference.set(upload, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        Toast.makeText(polls.this, "已上传", Toast.LENGTH_LONG).show();
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Toast.makeText(polls.this, "上传失败", Toast.LENGTH_LONG).show();
    }
});
英文:

your document needs to mention that this has to be merged instead replace.

.set(data, SetOptions.merge())

Try this if it works.

documentReference.set(upload, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;Void&gt; task) {
            Toast.makeText(polls.this, &quot;UPLOADED&quot;, Toast.LENGTH_LONG).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(polls.this, &quot;FAILED&quot;, Toast.LENGTH_LONG).show();
        }
    });

huangapple
  • 本文由 发表于 2020年8月20日 17:45:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502455.html
匿名

发表评论

匿名网友

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

确定