Exception when trying to write into a Realm : Closing with open read transactions

Hello, I’m trying to write an object into the realm, but I’m getting this error when performing write, getUserRealmInstance().writeBlocking.

I’ve tried :

  • Changing it to write instead of writeBlocking
  • Removing the lazy keyword
  • accessing by realm instead of getUserRealmInstance()

Error :
java.lang.IllegalStateException: [RLM_ERR_WRONG_TRANSACTION_STATE]: Closing with open read transactions

object UserRealm {
private val realmConfiguration: RealmConfiguration by lazy {
        RealmConfiguration
            .Builder(schema = setOf(User::class))
            .schemaVersion(2)
            .deleteRealmIfMigrationNeeded()
            .build()
    }

    private val realm : Realm by lazy {
        Realm.open(realmConfiguration)
    }

private fun getUserRealmInstance() : Realm {
        return this.realm
    }

suspend fun upsertRealmFromDB(userID : String?) : Boolean{


        if(userID == null && getRealmUser() == null) return false

        var doc : BsonDocument? = null

        if(userID == null) {
            doc = callGetUser(getRealmUser()!!.UserID)
        }else{

            doc = callGetUser(userID)
        }

        if(doc == null) return false

        val tempUser = User().apply {
            _id = doc["_id"] as BsonObjectId
            UserID = (doc["UserID"] as BsonString).value
            UserName = (doc["UserName"] as BsonString).value
            UserPhone = (doc["UserPhone"] as BsonString).value
            UserPoints = (doc["UserPoints"] as BsonInt64).value.toInt()
            UserDOB = RealmInstant.from((doc["UserDOB"] as BsonDateTime).value, 0)
            UserCreatedAt = RealmInstant.from((doc["UserCreatedAt"] as BsonDateTime).value, 0)
            UserLastLogin = RealmInstant.from((doc["UserLastLogin"] as BsonDateTime).value, 0)
            UserRole = (doc["UserRole"] as BsonString).value
        }


        try {

            val tempID = if(userID == null) getRealmUser()!!.UserID else userID


            val tempDOB : RealmInstant = RealmInstant.from((doc["UserDOB"] as BsonDateTime).value,0)
            val tempCreated : RealmInstant = RealmInstant.from((doc["UserCreatedAt"] as BsonDateTime).value,0)
            val tempUpdated : RealmInstant = RealmInstant.from((doc["UserLastLogin"] as BsonDateTime).value,0)



            getUserRealmInstance().writeBlocking {

                val result = this.query<User>("UserID == $0", tempID).find()

                val user = if(result.isEmpty())  null else result.last()

                if (user != null) {

                    Log.d("Realm-Update-User", "start updating user")
                    user.UserName = (doc["UserName"] as BsonString).value
                    user.UserPhone = (doc["UserPhone"] as BsonString).value
                    user.UserPoints = (doc["UserPoints"] as BsonInt64).value.toInt()
                    user.UserDOB = tempDOB
                    user.UserCreatedAt = tempCreated
                    user.UserLastLogin = tempUpdated
                    user.UserRole = (doc["UserRole"] as BsonString).value

                    Log.d("Realm-Update-User", "finished updating user")
                } else {

                    Log.d("Realm-Update-User", "starting creating user")
                    val newUser = copyToRealm(User().apply {
                        _id = doc["_id"] as BsonObjectId
                        UserID = (doc["UserID"] as BsonString).value
                        UserName = (doc["UserName"] as BsonString).value
                        UserPhone = (doc["UserPhone"] as BsonString).value
                        UserPoints = (doc["UserPoints"] as BsonInt64).value.toInt()
                        UserDOB = RealmInstant.from((doc["UserDOB"] as BsonDateTime).value, 0)
                        UserCreatedAt =
                            RealmInstant.from((doc["UserCreatedAt"] as BsonDateTime).value, 0)
                        UserLastLogin =
                            RealmInstant.from((doc["UserLastLogin"] as BsonDateTime).value, 0)
                        UserRole = (doc["UserRole"] as BsonString).value

                    })
                    Log.d("Realm-Update-User", "finished creating user")


                }



            }

            return true

        }catch(e : Exception){
            Log.e("Realm-Update-User",e.toString())
            return false
        }

    }