Loe
04/26/2023, 7:21 PME com.google.firebase.database.DatabaseException: Failed to gain exclusive lock to Firebase Database's offline persistence. This generally means you are using Firebase Database from multiple processes in your app. Keep in mind that multi-process Android apps execute the code in your Application class in all processes, so you may need to avoid initializing FirebaseDatabase in your Application class. If you are intentionally using Firebase Database from multiple processes, you can only enable offline persistence (i.e. call setPersistenceEnabled(true)) in one of them.
at com.google.firebase.database.android.SqlPersistenceStorageEngine.openDatabase(SqlPersistenceStorageEngine.java:861)
. . .
Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5 SQLITE_BUSY): , while compiling: PRAGMA journal_mode
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1068)
at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:811)
at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:419)
Loe
04/26/2023, 7:22 PMclass MyApplication : Application() {
override fun onCreate() {
super.onCreate()
this.initFirebaseApp() // This function is in shared/androidMain
}
}
// Located in located inside shared/androidMain
fun Context.initFirebaseApp() {
Firebase.initialize(this)
// enable offline caching
Firebase.database.setPersistenceEnabled(true)
}
Loe
04/26/2023, 7:23 PMThis generally means you are using Firebase Database from multiple processes in your app. Keep in mind that multi-process Android apps execute the code in your Application class in all processes, so you may need to avoid initializing FirebaseDatabase in your Application class. If you are intentionally using Firebase Database from multiple processes, you can only enable offline persistence (i.e. call setPersistenceEnabled(true)) in one of them.
The reason i’m initialize Firebase inside the Application instead of an Activity is because FirebaseAuth
is needed for my ViewModel [i’m using ViewModelProvider.Factory
]. Let me know if there is a way to provide ViewModel dependency from an Activity.
I found this stackOverflow post[https://stackoverflow.com/a/49806551/7234479] with similar issue and the answer says Firestore can't be acceded from a different thread than it own at the first write operation because is the time when the reference to the local db is created..
so I tried using Mutex, so one thread at a time tries initializing Firebase, inside my Application, but that didn’t fix the issue
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val mutex = Mutex()
synchronized(mutex) {
this.initFirebaseApp()
}
}
}
ayodele
04/26/2023, 11:12 PMLoe
05/10/2023, 5:16 PMApplication
to Activity
fixed the issue.