我是 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); } } });}
1 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
當您與 Firebase 實時數據庫交互時,您的應用程序和 Firebase 服務器之間會打開一個套接字連接。從那時起,應用程序和數據庫之間的所有流量都通過同一個套接字。因此,無論您創建實時數據庫實例多少次,它始終是單個連接。
另一方面,根據 @Frank van Puffelen 的評論,當您與 Cloud Firestore 數據庫交互時,客戶端使用 HTTP/2 連接而不是 Web 套接字連接到 Firestore。HTTP/2 和 Web 套接字都通過單個連接發送多個請求。
如果一段時間內沒有活動監聽器,Cloud Firestore 客戶端會自動關閉連接,但當您附加監聽器或再次執行讀/寫操作時,它會重新打開連接。
- 1 回答
- 0 關注
- 101 瀏覽
添加回答
舉報
0/150
提交
取消