java - 如何删除本地缓存中的可用状态?

标签 java android firebase google-cloud-firestore

所以我正在开发一个使用 firebase 的 firestore 的应用程序,我想知道这是否可行,因为我不希望我的应用程序检查服务器中不再存在的数据。

示例:

collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot snapshots, FirebaseFirestoreException e) {
            for (DocumentSnapshot snapshot : snapshots) {
                System.out.println(snapshot.getId());
                // This prints document IDs of documents that were deleted
                // from the collection when the app was not running
            }
        }
    });


使用 DocumentSnapshot.exists() 过滤仅存在于服务器中的快照不起作用


更多信息在 this page :

The initial state can come from the server directly, or from a local cache. If there is state available in a local cache, the query snapshot will be initially populated with the cached data, then updated with the server's data when the client has caught up with the server's state.

最佳答案

您可以通过检查其元数据来确定快照是否来自缓存。

QuerySnapshot#getMetadata()返回一个 SnapshotMetadata 对象。 SnapshotMetadata#isFromCache()如果快照来自缓存,则返回一个 boolean 值。

如果您想在元数据更改时收到通知(这样您就可以知道 isFromCache() 是否更改),那么您必须在添加监听器时传递选项:

   // Create options
   QueryListenOptions options = new QueryListenOptions().includeDocumentMetadataChanges();

   // Pass them when you add the listener
   collectionReference.addSnapshotListener(options, new EventListener<QuerySnapshot>() {
       // ...
   });

请参阅 addSnapshotListener 的文档

关于java - 如何删除本地缓存中的可用状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790741/

相关文章:

Java - 将带有时区的unix时间(以毫秒为单位)转换为时间戳

java - 单元测试在 Debug模式下终止

java - NetworkOnMainThreadException 尽管有 AsyncTask

ios - 使用 Firebase/Google Cloud Functions 推送通知

java - Maven - 在多模块项目中构建特定项目

java - 如何在 Eclipse 中运行项目?

android - 指定的 Firebase 路径超出了可写入的最大深度

android - 如何在 Release模式下检索 firebase 存储图像?

java - com.android.dex.DexIndexOverflowException : Error:Execution failed for task "...\Java\jdk1.7.0_17\bin\java.exe" finished with non-zero exit value 2

android - 无法访问类型为 ContactsXmpp 的封闭实例?