Hey guys, what is the best way to access Realm for...
# realm
c
Hey guys, what is the best way to access Realm for anywhere in the app? I don’t think calling the
Realm.open(getRealmUnEncryptedConfig())
every time is correct
i know to calling the realm for anywhere from app is not correct 😅
j
You can do something like https://github.com/joreilly/FantasyPremierLeague/blob/main/common/src/commonMain/kotlin/dev/johnoreilly/common/di/Koin.kt if you're using Koin (or similar if some other DI framework)
c
the project in a transitional stage to implement the dependency injection but for now we need to access the realm from different places
i found the solution with this class
Copy code
class RealmHelper {

    private val realmDB: Realm by lazy {
        RealmDB.initRealmUnencrypted()
    }

    private val realmEncDB: Realm by lazy {
        RealmDB.initRealmEncrypted()
    }

    fun getRealm(): Realm {
        return realmDB
    }

    fun getEncryptedRealm(): Realm {
        return realmEncDB
    }

    companion object {

        private lateinit var instanceVar: RealmHelper
        val sharedInstance: RealmHelper
            get() {
                if (!::instanceVar.isInitialized) {
                    instanceVar = RealmHelper()
                }
                return instanceVar
            }

    }
}