Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got lateinit property filesDir has not been initialized - RealmInitializer$Companion.getFilesDir(RealmInitializer.kt:34). Realm 2.0.0. #1799

Closed
KhoaChau0594 opened this issue Jul 8, 2024 · 2 comments

Comments

@KhoaChau0594
Copy link

KhoaChau0594 commented Jul 8, 2024

How frequently does the bug occur?

Always

Description

I am using Realm-kotlin Android version 2.0.0. I add Realm follow the https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/install/#std-label-kotlin-install-android. I did notice that this is for version 1.16.0 but cannot find instruction for version 2.0.0 ( I found one in the example but it used the kotlin multiplatform) and got error when create a Realm by this:

    lateinit var realm: Realm

    suspend fun createRealm(realmName: String) {
        val downloadResult = langDownloader.download(
            "link_to_realm",
            realmName
        )
        
        delay(5000)

        when(downloadResult) {
            is DownloadLangResult.Success -> {
                val configuration =
                    RealmConfiguration.Builder(setOf(StringResourceRealm::class))
                    .directory(downloadResult.directory)
                    .name(downloadResult.name)
                    .build()

                realm = Realm.open(configuration)
            }

            is DownloadLangResult.Failed -> {
                Toast.makeText(
                    context,
                    downloadResult.exception.message ?: "Pull language failed",
                    Toast.LENGTH_SHORT
                ).show()
                return
            }
        }
    }

I have make sure that the downloaded realm file is exists in the app's /files directory.
I get the same error when use
RealmConfiguration.create(setOf(StringResourceRealm::class))

Stacktrace & log output

kotlin.UninitializedPropertyAccessException: lateinit property filesDir has not been initialized
                                                 	at io.realm.kotlin.internal.RealmInitializer$Companion.getFilesDir(RealmInitializer.kt:34)
                                                 	at io.realm.kotlin.internal.platform.SystemUtilsAndroidKt.appFilesDirectory(SystemUtilsAndroid.kt:30)
                                                 	at io.realm.kotlin.RealmConfiguration$Builder.<init>(RealmConfiguration.kt:54)
                                                 	at com.staff.dynamic_langs.RealmDatabaseManager.createRealm(RealmDatabaseManager.kt:48)
                                                 	at com.staff.dynamic_langs.RealmDatabaseManager$createRealm$1.invokeSuspend(Unknown Source:15)
                                                 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
                                                 	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
                                                 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
                                                 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
                                                 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
                                                 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
                                                 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
                                                 	Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@3cce21c, Dispatchers.IO]

Can you reproduce the bug?

Always

Reproduction Steps

Project gradle:

plugins {
    ...
    id("io.realm.kotlin") version "2.0.0"               apply true
}

tasks.create<Delete>("cleanRP") {
    group = "rp"
    delete(
        fileTree(rootProject.buildDir)
    )
}

App gradle:

plugins {
    ...
    id("io.realm.kotlin")
}

dependencies {
     ,,,,
     // Realm
     Implementation("io.realm.kotlin:library-base:2.0.0")
}

How I create Realm:

class RealmDatabaseManager @Inject constructor(
    val langDownloader: LangDownloader,
    val context: Context,
) {
    lateinit var realm: Realm

    suspend fun createRealm(realmName: String) {
        val downloadResult = langDownloader.download(
            "scl/fi/p69qe33ylr5zr2e65f98z/vi.realm?rlkey=6fhjau9lgibbpr093gfb7zji0&st=smrxsn09&dl=0",
            realmName
        )
        
        delay(5000)

        when(downloadResult) {
            is DownloadLangResult.Success -> {
                val configuration =
                    RealmConfiguration.Builder(setOf(StringResourceRealm::class))
                    .directory(downloadResult.directory)
                    .name(downloadResult.name)
                    .build()

                realm = Realm.open(configuration)
            }

            is DownloadLangResult.Failed -> {
                Toast.makeText(
                    context,
                    downloadResult.exception.message ?: "Pull language failed",
                    Toast.LENGTH_SHORT
                ).show()
                return
            }
        }
    }
}

@AndroidEntryPoint
class HomeActivity : BaseViewBindingActivity3<ActivityHomeBinding>(), HomeActivitySearchHolder {
       override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        ...
        val realmManager = RealmDatabaseManager(
            LangDownloader(
                downloader = DownloadLangClient().client,
                context = this@HomeActivity,
            ),
            context = this@HomeActivity,
        )
        realmManager.createRealm("vi.realm")
    }

Version

2.0.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Samsung A15 OneUI 6.1 and Android Emulator Pixel 8 API 34

Build environment

Android Studio version: Android Studio Koala | 2024.1.1
Android Build Tools version: 8.5
Gradle version: 8.7

Copy link

sync-by-unito bot commented Jul 8, 2024

➤ PM Bot commented:

Jira ticket: RKOTLIN-1107

@KhoaChau0594
Copy link
Author

I manage to fix the error. It turn out the my app use Hilt for DI and require a custom AppInitializer.

<provider
      android:name="androidx.startup.InitializationProvider"
      android:authorities="${applicationId}.androidx-startup"
      tools:node="remove"/>

I guess Realm is not working with the custom AppInitializer so you have to add it manually by add:

val appInitializer = AppInitializer.getInstance(this)
appInitializer.initializeComponent(RealmInitializer::class.java)

to you Application onCreate().

I get this idea from this issue #1508, the answer of timonmw

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant