I want to disable Firestore offline persistence in Android using Kotlin. If the device is offline there will be no local cache to further sync the data automatically.
I have tried to disable Firestore offline persistence using:
val firestoreSettings = FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false) // Disable offline persistence
.build()
val db = FirebaseFirestore.getInstance()
db.firestoreSettings = firestoreSettings
But, setPersistenceEnabled is deprecated.
Then tried using:
val firebaseSettings = firestoreSettings {
setLocalCacheSettings(
memoryCacheSettings { }
)
}
val db = FirebaseFirestore.getInstance()
db.firestoreSettings = firebaseSettings
But it still uses offline persistence.
Indeed FirebaseFirestoreSettings.Builder#setPersistenceEnabled(value: Boolean) is deprecated. As you already have seen in the official documentation related to offline persistence in Firestore, the solution for enabling the offline persistence is:
val settings = firestoreSettings {
// Use memory cache
setLocalCacheSettings(memoryCacheSettings {})
}
db.firestoreSettings = settings
This means that if you're using the above lines of code, you'll be using the memory cache and not the disk cache.
According to your last comment:
It still uses offline persistence
I can't tell you why you're still reading the data from the local cache without seeing how you're actually reading the data. However, if you don't want to read the data from the cache but only from the Firebase servers, then I recommend you specify the source of the query by using Query#get(source: Source) which:
Attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached.