How to upsert correctly with kotlin-realm?


I am currently working on a Spring Backend which uses realm-kotlin to create a realm-database, it is later saved as a zip file and provided via api to apps.

I am working with “nested” RealmObjects with occure in multiple places of my “root” RealmObject. Those RealmObjects have PrimaryKeys

val config = RealmConfiguration.Builder(setOf(
            // classes

        val realm =
        realm.writeBlocking {
            data.forEach {
                this.copyToRealm(it, UpdatePolicy.ALL)

      // create zip  

However, I observe that the resulting realm db file is much larger than a realmdb file which was created with the swift sdk.
If I dont specify UpdatePolicy.ALL (it defaults to ERROR) the creation fails due to already existing objects with already existing primary keys which is expected. However, I guess that UpdatePolicy.ALL leads to massive updates on already existing objects, resulting in a large number of object versions?
If this is the case and the problem I am not sure how to solve it with realm-kotlin. I saw that the swift sdk has a UpdatePolicy.MODIFIED which only updates when the values actually differ which is what I would need.

The java sdk has also something similar.

I would greatly appreciate any hints regarding this issue

I find this sort of thing confusing, too. I’ve used the java and swift versions, but now I’m using the Kotlin version.

I’m also trying to understand what happens to the unnamed properties when I do an “upsert” operation as described. Say ‘_id’ is my primary key and I have two properties among many others, ‘firstName’ and ‘lastName’, all of which are initialized in its RealmObject based class.

In. the NameAddr object that I want to modify, neither firstName or lastName currently have the default values, which might have been “”. Instead I have “Joe” “Smith”, and I’ve discover his name is actually “Joel”. So within the writeBlocking I do:

_id = alreadyKnownRecordId
firstName = “Joel”
} , UpdatePolicy.ALL)

When the write completes, will the updated record retain the lastName “Smith” and all the other fields (which I might not know) retain their values as well. Or will unmentioned properties go back to their default values in the RealmObject class, meaning that lastName would become “”, etc,?