我正在使用 Firestore 數(shù)據(jù)庫創(chuàng)建一個(gè)聊天模塊。以下是我的監(jiān)聽器代碼,用于監(jiān)聽新消息: mDb.collection("Users_Collection").document(mAuth.getUid()).collection("Recipients") .document(psychichObj.getUid()).collection("Messages").orderBy("time").limit(30) .addSnapshotListener(MetadataChanges.INCLUDE, new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) { if (e==null){ for (DocumentChange dc:queryDocumentSnapshots.getDocumentChanges()){ switch (dc.getType()){ case ADDED: Log.d("chatevents", "onEvent:Added "); messgaesDataClass msg = dc.getDocument().toObject(messgaesDataClass.class); messages.add(msg); chatAdapter.notifyDataSetChanged(); messagesRecycler.smoothScrollToPosition(messages.size()); break; case REMOVED: Log.d("chatevents", "onEvent:Removed "); case MODIFIED: Log.d("chatevents", "onEvent:Modiefied "); } } } } });當(dāng)我發(fā)送消息時(shí),我想知道如何偵聽本地緩存中的 msg 對象或其元數(shù)據(jù)處于掛起狀態(tài)且尚未發(fā)送到服務(wù)器?
1 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
將MetadataChanges傳遞給 Query 的addSnapshotListener(MetadataChanges metadataChanges, EventListener listener)方法時(shí):
指示僅元數(shù)據(jù)更改(即僅 Query.getMetadata() 更改)是否應(yīng)觸發(fā)快照事件。
這基本上意味著每次元數(shù)據(jù)更改時(shí),都會觸發(fā)偵聽器。因此,在掛起操作的情況下,變量的值pending
將被更改。
我想知道如何監(jiān)聽本地緩存中的 msg 對象
在這種情況下,您應(yīng)該考慮使用 SnapshotMetadata 的isFromCache()方法:
Log.d(TAG, "isFromCache: " + documentSnapshot.getMetadata().isFromCache());
當(dāng)我在線時(shí),它會打?。?/p>
isFromCache: false
當(dāng)我離線時(shí),它會打?。?/p>
isFromCache: true
添加回答
舉報(bào)
0/150
提交
取消