Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-1028

Does Realm creation happen on UI Thread?

      How frequently does the bug occur?

      Always

      Description

      I have this very simple class that handles realm initialization and access

      import io.realm.kotlin.Realm
      import io.realm.kotlin.RealmConfiguration
      import io.realm.kotlin.types.RealmObject
      import io.realm.kotlin.types.TypedRealmObject
      import kotlinx.coroutines.flow.map
      import kotlin.reflect.KClass
      
      class Database {
          private val db: Realm
      
          init {
              val config = RealmConfiguration.create(schema = setOf(LocalUser::class))
              db = Realm.open(config)
          }
      
      
      
          suspend fun add(manga: RealmObject) {
              db.write {
                  copyToRealm(manga)
              }
          }
      
          fun <T : TypedRealmObject> queryFirstLive(clazz: KClass<T>) =
              db.query(clazz).first().asFlow().map { it.obj }
      
          suspend fun deleteAll() {
              db.write {
                  deleteAll()
              }
          }
      
          fun close() {
              if (!db.isClosed())
                  db.close()
          }
      
      }
      
      

      If i use StrictMode like this in my Main Application class

       private fun setupStrictMode() {
              val policy = ThreadPolicy.Builder()
                  .detectDiskReads()
                  .detectDiskWrites()
                  .penaltyDeath() // This will cause the app to crash on violation
                  .build()
              StrictMode.setThreadPolicy(policy)
          }
      

      It informs me that realm creation (in the init block) is violating the diskWrite policy . While it shows absolutely no visible performance effects on the app , i was just wondering if I am doing things right ?

      In the realm documentation too I do not find any examples in which the realm creation is offloaded into some background thread.

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Always

      Reproduction Steps

      No response

      Version

      1.11.1

      What Atlas App Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Android 13

      Build environment

      Android Studio version: ... Hedgehog
      Android Build Tools version: ...
      Gradle version: ... 8.2.2

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: