Hi guys, I’ve KMM project that uses Firebase datab...
# multiplatform
l
Hi guys, I’ve KMM project that uses Firebase database [https://github.com/GitLiveApp/firebase-kotlin-sdk] and i’m getting an exception when offline caching is enabled. This issue only happens on the first app install, and doesn’t not occur after that:
Copy code
E  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)
I initialize Firebase and set offline caching from `Application`:
Copy code
class 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)
}
The exception says the ff:
Copy code
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.
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
Copy code
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        val mutex = Mutex()
		synchronized(mutex) {
			this.initFirebaseApp()
		}
    }
}
a
Apart from setting persistent, are you initializing any other firebase in your application class??
l
moving the Firebase initialization from
Application
to
Activity
fixed the issue.
570 Views