Documents deleted with a function don't update the local cache if persistence is enabled.
I am running the firebase emulator. I call a firebase function that deletes an entire collection of documents. The collection disappears from the emulator and when persistence is not enabled, the documents are now gone from query results.
However, when persistence is enabled, Firestore returns those documents when queried, regardless of the specified source. For example, if I specify server as the source, the resulting list of documents still contains those deleted documents. The query metadata indicates that the query results are from the server, but that is not the case. Here is the metadata when source is set to server: { fromCache: false, hasPendingWrites: false }. This is correct, but the collection of returned docs is not.
If I disable persistence, the problem goes away and I am returned the fresh collection - empty as expected.
This is a pretty serious bug that prevents anyone from using Firestore persistence in conjunction with firebase functions that manipulate document collections.
source set to server.{ fromCache: false, hasPendingWrites: false } but the collection of documents includes all of the documents deleted by the function.I can't seem to get the latest results from that collection (I expect to receive an empty result) unless I disable persistence, at which point the query result returns an empty list, as expected.
The issue seems to be directly related to the emulator. Once the function is deployed and I read the docs from the remote firestore, everything seems to work as expected.
Sorry to hear about your trouble. I was able to reproduce this with an emulator using the reproduction steps you outlined, will post back once I have more information.
Update: this appears to be a bug in the emulator. It may be triggered by deleting documents via the emulator UI as well. I will reroute to somebody working on the emulator (the internal bug is b/170145142). Thank you for reporting the issue!
As per the documentation, local persistence should be turned off when working with the Firestore Emulator, exactly for reasons you've discovered.
Note: The Cloud Firestore emulator clears database contents when shut down. Since the offline cache of the Firestore SDK is not automatically cleared, you may want to disable local persistence in your emulator configuration to avoid discrepancies between the emulated database and local caches; in the Web SDK, persistence is disabled by default.
I can definitely look into the issue when I have more bandwidth, but is there any reason that you want persistence to be enabled?
Hey @SDrinkwater. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Hi, please don't close this. The reason this needs to be fixed, is so that local tests have integrity.
Hey @SDrinkwater. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!