Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import io.realm.kotlin.MutableRealm
import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -34,18 +36,19 @@ class RealmWrapperImpl @Inject constructor(
// https://www.mongodb.com/docs/realm/sdk/kotlin/realm-database/frozen-arch/#thread-safe-realms
private lateinit var realm: Realm
private lateinit var config: RealmConfiguration
private val mutex = Mutex()

private fun getRealm(): Realm {
private suspend fun getRealm(): Realm {
if (!this::realm.isInitialized) {
config = createAndSaveRealmConfig()
realm = createRealm()
}
return realm
}

private fun createRealm(): Realm {
private suspend fun createRealm(): Realm = mutex.withLock {
Simber.d("[RealmWrapperImpl] getting new realm instance", tag = REALM_DB)
return try {
try {
try {
Realm.open(config)
} catch (ex: IllegalStateException) {
Expand Down