0

I need to set off the persistence in a certain document in Firestore; but all other documents should work with default configuration like this implementation. I have implemented the code; but calling firestoreSettings again causing this error in below:

java.lang.IllegalStateException: FirebaseFirestore has already been started and its settings can no longer be changed. You can only call setFirestoreSettings() before calling any other methods on a FirebaseFirestore object. at com.google.firebase.firestore.FirebaseFirestore.setFirestoreSettings(com.google.firebase:firebase-firestore@@17.0.5:140)

How can I achieve to set setPersistenceEnabled=false for a specific document or is it possible? Thanks in advance.

Orcun Sevsay
  • 1,310
  • 1
  • 14
  • 44
  • What are you trying to do that requires persistence to be disabled for a single document? – Doug Stevenson Sep 27 '18 at 21:15
  • There is a document which is used for tracking driver's location in real-time. This document has a GeoPoint field. When driver's internet connection is offline, write operations are cached for later to sync with firestore. After the connectivity is restored and back online again, driver can send old geo-points that is irrelevant; because driver should be marked in real-time on MapView. I think that if I had a choice to choose a document to disable persistence, all those accumulated write operations won't be sent all at once. – Orcun Sevsay Sep 28 '18 at 07:42
  • By the way, I know that I only can send those write operations if the device has an internet connection and simply ignore to sync with firestore if device connectivity is offline. It is just a curiosity of mine that firestore could do something like this. – Orcun Sevsay Sep 28 '18 at 07:42

2 Answers2

1

Disk persistence can only be enabled for the entire Firestore client, before performing any other operations. It cannot be enabled/disabled for specific collections, queries or documents.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So in case we have a large DB (eg. 1 - 2GB), our web application would download the whole database to the client's device to make it available offline. This for many users might be overkilling. Are there any best practices to tackle this case other than prompt, if possible, the user whether (s)he want to have the data available offline? – Francesco Dec 20 '18 at 07:43
  • 1
    Only documents that you actually read will be persisted locally, but beyond that you have no control over the disk cache. – Frank van Puffelen Dec 20 '18 at 14:03
1

The way offline persistence works is by synchronizing the current state of each document written while offline. It doesn't synchronize the entire history of all the writes. The history of writes is irrelevant - what matters is only the current values in the document. Therefore, enabling persistence is not as expensive as you might be expecting.

As Frank said, you can't selectively choose to synchronize only some documents. It's either entirely off or on. Personally, I don't think you have a compelling case to disable it.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Doug: in my case, I know it's not the proper use case, but I have code that needs to go into production so I'm disabling persistence as a workaround for the slow queries issue (https://stackoverflow.com/a/46828867/2344535) . Ideally, only one Activity would need the workaround. So my question is really: is there any other solution for the slow queries issue? – rednuht Jan 15 '19 at 14:17
  • @doug-stevenson One case could be if I am storing some critical information (billing, payment, secret keys etc) in some collection. – Kushagra Gour Mar 04 '19 at 16:05