我是 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);
}
}
});
}
RISEBY
相关分类