Search code examples
kotlinrealm

Realm Kotlin SDK: Cannot modify managed List outside of a write transaction


I do not understand why I'm getting this error. I'm doing everything in a write transaction right?

activeSchoolClassNotNull { activeSchoolClass ->
    val schoolSubjects = ocjeneSkoleScraper.getSubjects()
    realm.write {
        activeSchoolClass.schoolSubjects = schoolSubjects.toRealmList()
        copyToRealm(activeSchoolClass, updatePolicy = UpdatePolicy.ALL)
        [email protected] = activeSchoolClass
    }
}

Can anyone tell me what I'm doing wrong?


Solution

  • The problem was that I needed to get the latest object from realm before modifying it.

    realm.write {
        val latestObj = findLatest(obj)
        latestObj.someProperty = "changed"
        copyToRealm(latestObj)
    }