我如何删除本地缓存中的可用状态? [英] How can I delete the available state in a local cache?

查看:185
本文介绍了我如何删除本地缓存中的可用状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


示例:


  collectionReference.addSnapshotListener(new EventListener< QuerySnapshot>(){
@Override
public void onEvent(QuerySnapshot snapshots,FirebaseFirestoreException e){
for(DocumentSnapshot snapshot:snapshots){
System.out.println(snapshot.getId());
//打印当应用程序未运行时,文件的ID被删除
//从集合中删除
}
}
});



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



更多信息在本页


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



解决方案

您可以通过检查其元数据来确定快照是否来自缓存。 p> QuerySnapshot#getMetadata( ) 返回一个 SnapshotMetadata 对象。
SnapshotMetadata#如果快照来自缓存,则isFromCache() 将返回一个布尔值。



如果您希望在元数据更改时收到通知(所以你可以知道 isFromCache()变化),那么当你添加监听器的时候你必须传递选项:

  //创建选项
QueryListenOptions options = new QueryListenOptions()。includeDocumentMetadataChanges();

//添加侦听器时传递它们
collectionReference.addSnapshotListener(options,new EventListener< QuerySnapshot>(){
// ...
}) ;

请参阅 addSnapshotListener


So I am working on an app that uses firebase's firestore and was wondering if this is possible because I don't want my app to check for data that no longer exists in the server.

Example:

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
            }
        }
    });


Using DocumentSnapshot.exists() to filter snapshots existing only in the server does not work


More info in 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.

解决方案

You can determine if the snapshot comes from the cache by checking its metadata.

QuerySnapshot#getMetadata() returns a SnapshotMetadata object. SnapshotMetadata#isFromCache() returns a boolean if the snapshot is from the cache.

If you want to be notified whenever the metadata changes (so you can know if isFromCache() changes) then you have to pass options when you add the listener:

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

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

See the docs for addSnapshotListener

这篇关于我如何删除本地缓存中的可用状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆