【问题标题】:How can I delete the available state in a local cache?如何删除本地缓存中的可用状态?
【发布时间】:2018-03-29 04:19:47
【问题描述】:

所以我正在开发一个使用 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:

初始状态可以直接来自服务器,也可以来自本地缓存。如果本地缓存中有可用的状态,查询快照将首先填充缓存数据,然后在客户端赶上服务器状态时使用服务器数据进行更新。

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

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

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

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

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

    请参阅addSnapshotListener 的文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多