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