gpt4 book ai didi

c# - 通过 DocumentReference 监听 firestore 时将打开多少套接字

转载 作者:IT王子 更新时间:2023-10-29 06:52:20 24 4
gpt4 key购买 nike

我是 Cloud Firestore 的新手。当我阅读文档时,我看到了以下代码:

DocumentReference docRef = db.Collection("cities").Document("SF");
FirestoreChangeListener listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> city = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in city)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});

实际上我知道如何通过过滤查询监听并监听查询快照中的所有 300 条记录,但是即使只有一个文档更新,查询也会读取所有记录并且它会显着增加读取计数(成本也是如此)。

如果我有 300 个文档并且我想通过文档引用快照收听所有文档的实时更新怎么办?是否会有 300 个独立的套接字或一个单独的套接字来监听所有这些套接字。 C# 驱动程序和 Flutter 驱动程序的行为是否相同?

实现会是这样;

foreach (var docRef in docRefList) //300 records
{
FirestoreChangeListener listener = docRef.Listen(snapshot =>
{
Console.WriteLine("Callback received document snapshot.");
Console.WriteLine("Document exists? {0}", snapshot.Exists);
if (snapshot.Exists)
{
Console.WriteLine("Document data for {0} document:", snapshot.Id);
Dictionary<string, object> city = snapshot.ToDictionary();
foreach (KeyValuePair<string, object> pair in city)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
}
});
}

最佳答案

当您与 Firebase 实时数据库交互时,您的应用程序和 Firebase 服务器之间的单个 套接字连接会打开。从那一刻起,应用程序和数据库之间的所有流量都通过同一个套接字。因此,无论您创建实时数据库实例多少次,它始终是单个连接。

另一方面,根据@Frank van Puffelen 的评论,当您与 Cloud Firestore 数据库交互时,客户端使用 HTTP/2 连接而不是网络套接字连接到 Firestore。 HTTP/2 和网络套接字都通过单个连接发送多个请求。

如果一段时间内没有活跃的监听器,Cloud Firestore 客户端会自动关闭连接,但当您附加监听器或再次执行读/写操作时,它会重新打开连接。

关于c# - 通过 DocumentReference 监听 firestore 时将打开多少套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57590579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com