我如何删除本地缓存中的可用状态? [英] How can I delete the available state in a local cache?
问题描述
示例:
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>(){
// ...
}) ;
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屋!