你不需要 beta SDK 或 hack 来实现这一点,我找到了解决方案,实际上非常简单。
The https://firebase.google.com/docs/firestore/query-data/listen https://firebase.google.com/docs/firestore/query-data/listen文档不包含 Go 的示例。
Go 的 firestore 客户端 API 的源代码有一个未导出的 watchStream,我们无法直接使用:https://github.com/googleapis/google-cloud-go/blob/master/firestore/watch.go#L130 https://github.com/googleapis/google-cloud-go/blob/master/firestore/watch.go#L130
对存储库的深入搜索表明,这实际上在 DocumentSnapshotIterator 和 QuerySnapshotIterator 上使用:https://github.com/googleapis/google-cloud-go/blob/master/firestore/docref.go#L644 https://github.com/googleapis/google-cloud-go/blob/master/firestore/docref.go#L644 and: https://github.com/googleapis/google-cloud-go/blob/master/firestore/query.go#L716 https://github.com/googleapis/google-cloud-go/blob/master/firestore/query.go#L716.
The Collection
包含一个Snapshots
方法返回我们想要的快照迭代器,之后一切都很简单,我们只需对其进行一个不定循环Next
method.
Example:
cols, err := client.Collections(context.Background()).GetAll()
for _, col := range cols {
iter := col.Snapshots(context.Background())
defer iter.Stop()
for {
doc, err := iter.Next()
if err != nil {
if err == iterator.Done {
break
}
return err
}
for _, change := range doc.Changes {
// access the change.Doc returns the Document,
// which contains Data() and DataTo(&p) methods.
switch change.Kind {
case firestore.DocumentAdded:
// on added it returns the existing ones.
isNew := change.Doc.CreateTime.After(l.startTime)
// [...]
case firestore.DocumentModified:
// [...]
case firestore.DocumentRemoved:
// [...]
}
}
}
}
您的,格拉西莫斯·马罗普洛斯,又名@kataras https://github.com/kataras/iris