简体   繁体   English

更新索引值时,Firestore addSnapshotListener 无法收听文档更新

[英]Firestore addSnapshotListener could not listen to update on document when updating the index value

I am not quite sure if I am doing this correctly in Firebase in Swift. Say I store some date in a collection called "temp", and I am trying to listen to any update on the document.我不太确定我在 Firebase 和 Swift 中是否正确执行此操作。假设我将一些日期存储在名为“temp”的集合中,并且我正在尝试收听文档的任何更新。 Each document has couple fields.每个文档都有几个字段。 For instance, one field is userId, and another one is updateTs.比如一个字段是userId,另一个字段是updateTs。 In order to query the most recent documents, my listener is like this为了查询最近的文档,我的监听器是这样的

messageListener = Firestore.firestore().collection("temp")
                .whereField("userId", isEqualTo: "\(userId)")
                .whereField("updateTs", isLessThan: lastUpdateTs)
                .order(by: "updateTs", descending: true)
                .limit(to: 20)
                .addSnapshotListener { querySnapshot, error in
                    guard let snapshot = querySnapshot else {
                        return
                    }
                    for i in 0..<snapshot.documentChanges.count {
                        var change = snapshot.documentChanges[i]
                        // do things herer
                        if (change.type == .added) { print("added") }
                        if (change.type == .removed) { print("removed") }
                        if (change.type == .modified) { print("modified") }
                    }
                }

Since I am querying using both whereField and order, i have to create an composite index (userId + updateTs).由于我同时使用 whereField 和 order 进行查询,因此我必须创建一个复合索引 (userId + updateTs)。

Everything works well with the initial launch, however, when I try to update the values on the updateTs field while the userId field is unchanged using一切都适用于初始启动,但是,当我尝试更新 updateTs 字段上的值而 userId 字段未更改时使用

let messageData: [String: Any] = ["userId": userId, "updateTs": updateTs]
Firestore.firestore().collection("temp").document(thisDocument).updateData(messageData)

The above listener identifies the above change as removed instead of modified , and after the first time the listener observes the change, it will no longer be able to observe the change any more(probably due to it was identified as removed )上面的监听器将上面的变化标识为removed而不是modified ,监听器第一次观察到变化后,将无法再观察到变化(可能是因为它被标识为removed

My hypothesis is that it is because I am updating the value for an index, which causes this issue.我的假设是,这是因为我正在更新索引的值,这导致了这个问题。 In this case, is there any workaround?在这种情况下,有什么解决方法吗? Any help is appreciated任何帮助表示赞赏

What is the value of updateTs that you set on the document?您在文档上设置的updateTs的值是多少?

My guess is that you're setting updateTs to va value larger than lastUpdateTs .我的猜测是您将updateTs设置为大于lastUpdateTs的 va 值。 In that case the updated document no longer matches your query, and is removed from the query results.在这种情况下,更新后的文档不再匹配您的查询,并从查询结果中删除 So that would mean change.type is .removed as the document was removed from the query results.所以这意味着change.type.removed因为文档已从查询结果中删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:更新单个文档时,StreamBuilder 获取其他 Firestore 文档 - Flutter: StreamBuilder gets other firestore documents when updating a single document 如何在 Firestore 中更改文档数组字段的某个索引的值? - How to change the value of a certain index of an array field of a document in Firestore? 如果值未更改,Firestore 文档更新请求是否会运行? - Does Firestore document update request run if the value didn't change? 监听 Firestore 上的文档更改并为 StreamBuilder 创建对象的 stream - Listen to document changes on Firestore and create a stream of objects for StreamBuilder 使用 REST api 更新 Firestore 文档 - Firestore document update using REST api 从第 3 方 API 收集数据时如何更新 Firestore 文档而不是创建新文档 - How to update Firestore document when data changes when collected from 3rd Party API instead of creating new document 从 Firestore 获取文档时的错误处理 - Error handling when getting document from Firestore 使用云功能 typescript 在 Firestore 中创建子子集合时如何更新子集合文档 - How to update a sub-collection document when sub sub-collection is created in firestore using cloud functions typescript 两个重叠的 Firestore 侦听器是否会为同一文档更新账单? - Do two Firestore listeners that overlap bill for the same document update? Firestore 规则 - 更新具有 FieldValue.delete() 的文档 - Firestore Rules - Update document that has FieldValue.delete()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM