how to delete specific firestore document in Source.CACHE? - firebase

I can specify the source If I want to get data from firestore, either default, from server or from cache like this:
query.get(Source.CACHE)
say I normally get 50 documents if using the code above.now I want to delete one specific document from that query in cache, so I want to get only 49 documents.
how to do that ? is it possible ?

You don't have direct control over the status of the internal cache maintained by the Firebase SDKs. You can't delete or invalidate specific items.
The cache is completely managed by the SDK. The SDK determines when documents should be added or removed, based on whether or not they are in sync with the server.
You can use the SDK to delete a document, which will remove it from cached results. But that delete will eventually get synchronized with the server, and that's a permanent change.
If you have steps that show that the internal cache is somehow buggy or inconsistent with the contents of the server, then file a bug report with Firebase support that shows now to reproduce the issue.

I want to delete one specific document from that query in cache, so I want to get only 49 documents.
There is no way you can specify that in a delete() operation. If you want to get that behavior, your device should be offline. In that way, Firestore will delete the record from the local cache. But please note, that once you regain the connection, your record will also be deleted from Firebase servers. While online, there is no way you can have a deleted document in the cache and not from Firebase servers.

Related

How can I use local cache and only update changed documents using Firestore?

I'm working on a calendar app and I'm currently fetching the data from firestore to populate the calendar. Eventually, there will be a lot of data to be fetched and I'm trying to understand the caching system of Firestore but can't get behind it.
What I ideally want to achieve:
Always use cached data and only update those documents, which are new, edited or deleted.
How would I achieve that?
If you want to force the SDK to read from the cache, you can specify source options when calling get().
If you find yourself doing this everywhere though, it might make sense to consider using another database than Firestore as that is primarily an online, cloud-hosted database that continues to work while you're temporary offline.

Forcing an "Offline" Mode in Firestore

We are building an app for our teams out in the field that they collect their daily information using Firebase. However one of our concerns is poor connectivity. We are looking to build an Online/Offline button they can click to essentially work offline for when things slow down. We've built a workflow in which we query all the relevant information from Firestore.
I wanted to know if there was a way to tell Firestore to work directly on the cache only and not try to hit the servers directly. I don't want Firestore attempting to make server calls until they enable online again.
You shouldn't need to do this. If you use realtime listeners, they will already first return the data from the local cache, and only then reach out to the server to check for updates.
If you are performing one-time reads, the SDK will by default try to reach the server first (since it has only one chance to give you a value). If you want it to only check the local cache, you can pass an argument to the get call to do so.
You can also disable the network completely, in which case the client will never call on the network and only serve from the local cache. I recommend reading about that and more in the documentation on using Firestore offline.

Firestore Offline: Make sure a specific Document is always cached

Lets say I have a workout app, where every workout is one DocumentSnapshot. I want to have a donwload button, that downloads a workout/document.
I'm already using firestore's offline capabilities, but I need to ensure, that when I have downloaded this document, it is always available when opening the app without a connection.
So is it possible to ensure, that a specific document is always being cached in the local firestore cache?
I could also just persist the data of the DocumentSnapshot, the problem with this is, I can't update the Document and have the changes being synchronized with the "online" database when reconnecting with the wifi.
Is there any good way to achieve this?
So is it possible to ensure, that a specific document is always being cached in the local firestore cache?
It's not possible to ensure 100% of the time. The local cache is fully managed by the Firestore SDK. You don't have control over how it chooses to evict data from the cache. Any given cached document might be removed to make room for other documents in the future, if the cache becomes full.
Also, the cached document will not stay in sync with whatever is on the server, unless you write code to periodically query for (or listen for changes) in that document.
The functionality you're describing is best implemented with application code (probably with its own persistence layer) that specifically meets the needs of your app. The Firestore SDK won't do it for you.

Pricing: is multiple field changes in a firestore document cache seen as a single write when device is online?

I have a use case where I log each activity open count in a firestore document (activity_name->field, and count->value)... So I wanted to know, when the user is offline and each of his activity navigation is stored in firestore cache, as soon as the user gets online and firebase sdk syncs the changes to the main database, does firestore record the synced changes as a single write or it sees the various individual field changes since the last change and record as multiple writes?
The writes are queued up in the client and delivered individually, so there will be a cost of one write for each document that was written offline.
The important issue here is not so much the billing as it is the evaluation of security rules. It's entirely possible a series of 5 writes might actually only result in 4 successful writes and 1 failure due to the violation of a security rule. If those writes were actually compressed into a single write, that would potentially cause everything to fail, which would be undesirable (you'd likely want as many writes to succeed as possible).

How a process running SQLite knows that a particular page has been updated by another process?

How 2 independent SQLite cache modules get notified about the change in DB. More specifically, how the cache module know that a page has to be fetched from disk, as its content has been updated in DB by some other process.
SQLite writes all changed pages when a transaction finishes; once another connection is allowed to read, there are no dirty pages.
To detect changes made by other connections, there is the file change counter in the database header. However, it does not apply to specific pages but to the entire database.

Resources