首页 > 解决方案 > Data on Firestore console differs from the retrieved when consulting the database

问题描述

I deleted a document through firebase console, but then, when I run my project and it looks for that document, it retrieves the deleted document (that i can´t see in the console).

App after receiving the docSnapshot and parsing the json in it to retrieve the data of the deleted document

Firebase console showing there's no data saved in Cloud Firestore

Not sure if deleting the document is what triggered this problem.

Tried creating a new document with the same id, and see if then, the app would get this new document instead of the deleted one. There was no use, still bringing the deleted document.

It seems like the data used to display my collections and documents on the console, is not the same as the one that is consulted when running my project.

I reported this as a bug, but, has anyone experienced something similar?, how did you fix it?

标签: androidfirebasegoogle-cloud-firestorefirebase-console

解决方案


This is not a bug. The Firestore client SDKs have a feature called offline persistence that is enabled by default in Android and iOS. This feature caches your data so your app can access it even if the device is offline. You can specify the data source for a query by passing a source argument to the get() method.

documentReference.get(Source.SERVER)
// or
query.get(Source.SERVER)

if you are using a SnapshotListener and the document exists in cache, the first result will always come from the cache. You can check it by doing this:

documentReference.addSnapshotListener(MetadataChanges.INCLUDE) { snap, e ->
    snap?.metadata?.isFromCache
}

推荐阅读