Android Kotlin getting CLIENT_BAD_CHANGESET

Hi, i am using MongoDB realm client SDK in android in kotlin language.
i am setting up everything as docs in https://docs.mongodb.com/realm/sdk/android/
and try to sync my offline data to MongoDB atlas

class AppDelegate : Application() {
...
private fun initRealm() {
        // init realm instance
        Realm.init(this)

        // init realm online
        taskApp = App(AppConfiguration.Builder(BuildConfig.MONGODB_REALM_APP_ID)
            .defaultSyncErrorHandler { session, error ->
                Log.e(TAG(), "Sync error: ${error.errorMessage}, ${error.errorCode}, ${error.category.name}")
            }
            .build())
    }
...
}

object RealmOnline {
    fun config(): SyncConfiguration {
        val user = taskApp.currentUser()
        return SyncConfiguration.Builder(user, RealmApp.realmName)
            .allowQueriesOnUiThread(true)
            .allowWritesOnUiThread(true)
            .build()
    }

    fun getInstance(callBack: Realm.Callback): RealmAsyncTask = Realm.getInstanceAsync(config(), callBack)

}

and here is my models

open class User(
    @PrimaryKey var _id: ObjectId = ObjectId(),
    @Index var authId: String? = "",
    @Required var name: String? = "",
    var email: String? = ""
) : RealmObject()

open class Company(
    @PrimaryKey var _id: ObjectId = ObjectId(),
    @Index var id: String = Calendar.getInstance().timeInMillis.toString(),
    @Required var name: String = "",
    var user: User? = null,
    var primary: Boolean = false
) : RealmObject()

and my schema on mongodb.com

{
  "title": "Company",
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "id": {
      "bsonType": "string"
    },
    "primary": {
      "bsonType": "boolean"
    },
    "name": {
      "bsonType": "string"
    },
    "user": {
      "bsonType": "objectId"
    },
    "realm_partition": {
      "bsonType": "string"
    }
  },
  "required": [
    "realm_partition"
  ]
}
....

{
  "title": "User",
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "authId": {
      "bsonType": "string"
    },
    "email": {
      "bsonType": "string"
    },
    "name": {
      "bsonType": "string"
    },
    "realm_partition": {
      "bsonType": "string"
    }
  },
  "required": [
    "realm_partition"
  ]
}

The Problem is when i call RealmOnline.config() there are two errors:
1- Failed to transform received changeset: Schema mismatch: ‘Company’ has primary key ‘_id’, which is nullable on one side, but not the other

2- Sync error: Bad changeset (DOWNLOAD), CLIENT_BAD_CHANGESET(realm::sync::Client::Error:112), FATAL

How can i solve these problem?

As mentioned in the 1st error message, Company._id is nullable on one side and required on the other. Looking at your models, it looks like it’s a required property in Kotlin, but a nullable field in your Json Schema. You need to align your class definitions to match.

1 Like

thank you very much, it solved by adding

"required": [
    "_id",
    "id",
    "realm_partition",
    "name",
    "primary"
  ]

in company schema

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.