https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
t

Tung97 Hl

09/07/2023, 7:31 AM
Hi everyone, I have a class DBManager, and inside it I put a companion object:
Copy code
companion object : SynchronizedObject() {
        private val instanceRef: AtomicRef<DBManager?> = atomic(null)
        fun create(driver: SqlDriver) = synchronized(this) {
            if (instanceRef.value == null) {
                val localdb = LocalDb.invoke(driver)
                val instance = DBManager(localdb)
                instanceRef.update { instance }
                Logger.d("singleton", "$this")
            }
        }

        fun get(): DBManager = synchronized(this) {
            Logger.d("singleton", "$this")
            instanceRef.value ?: throw IllegalStateException("DBManager was not created")
        }
    }
Problem: When I log the companion object instance in create() and get() functions, it's the same in android/JVM, but it's different in kotlin/native (ios)
DBManager.Companion@c50300a0
and
DBManager.Companion@9e050250
I have already created the instance in iOS using this code in the init block:
Copy code
DBManager.companion.create(driver: CreateDBDriverKt.createDBDriver())
That makes my application always crash in iOS. Please help!
3 Views