使用SetOptions.merge()时,为什么我的Cloud Firestore交易未合并? [英] Why is my Cloud Firestore Transaction not merging when using SetOptions.merge()?

查看:48
本文介绍了使用SetOptions.merge()时,为什么我的Cloud Firestore交易未合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用Firestore并使用 SetOptions.merge() 在这样的Cloud Firestore交易中,我想没什么特别的:

Just started with Firestore and use the SetOptions.merge() in a Cloud Firestore Transaction like this nothing special I guess:

final Map<String, Object> visitorMap = new HashMap<>();
visitorMap.put(Visitor.NOTIFY_ON_CHAT_MESSAGE, true);
visitorMap.put(Visitor.TIME, FieldValue.serverTimestamp());
final DocumentReference docRefVisitor = mFirestore
        .collection(VISITORS)
        .document(theId)
        .collection(VISITORS_USER)
        .document(getCurrentUser().getUserId());
mFirestore.runTransaction(new com.google.firebase.firestore.Transaction.Function<void>() {
    @Nullable
    @Override
    public void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
        transaction.set(docRefVisitor, visitorMap, SetOptions.merge());
    }
})

文档说:

如果文档不存在,将创建该文档.如果文件 确实存在,其内容将被新提供的内容覆盖 数据,除非您指定应将数据合并到 现有文件

If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document

我发现Visitor.NOTIFY_ON_CHAT_MESSAGE boolean正在覆盖Cloud Firestore数据库文档中的现有boolean.我虽然SetOptions.merge()不会覆盖现有值吗? 也许我错过了有关交易的工作方式的一些知识,或者这与Beta相关CF是Beta版本的事情

I experience that Visitor.NOTIFY_ON_CHAT_MESSAGE boolean is overwriting existing boolean at Cloud Firestore database Document. I though the SetOptions.merge() would not overwrite existing values? Maybe I missed something about how Transaction works or this is a Beta related thing since CF is Beta

推荐答案

SetOptions merge()方法视为

更改set()调用的行为,以仅替换其data参数中指定的值. set()调用中省略的字段将保持不变.

Changes the behavior of set() calls to only replace the values specified in its data argument. Fields omitted from the set() call will remain untouched.

因此,SetOptions.merge()方法将仅替换fieldPaths下的字段.在fieldPaths中未指定的任何字段都将被忽略,并且保持不变.

So SetOptions.merge() method will only replace the fields under fieldPaths. Any field that is not specified in fieldPaths is ignored and remains untouched.

结论是,如果文档不存在,则会创建该文档.如果文档确实存在,则其内容will be overwritten包含新提供的数据,除非您指定将数据合并到现有文档中,如下所示:

As a conslusion, if the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document, as follows:

// Update one field, creating the document if it does not already exist.
Map<String, Object> data = new HashMap<>();
data.put("Visitor.NOTIFY_ON_CHAT_MESSAGE", true);
docRefVisitor.set(data, SetOptions.merge());

这篇关于使用SetOptions.merge()时,为什么我的Cloud Firestore交易未合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆