如何删除本地缓存中的可用状态?
问题描述:
所以我正在研究一个使用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工作
更多信息:
初始状态可以来从服务器直接或从本地缓存。如果本地缓存中存在可用状态,则查询快照将首先填充缓存的数据,然后在客户端赶上服务器状态时使用服务器数据更新。
答
您可以通过检查其元数据来确定快照是否来自缓存。
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