[英]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.