3 / 3
Jul 2024

0

I have a Kotlin Multiplatform app that I’m trying to get to work with Realm/Atlas. When I try to do a Realm.open(it) it says:

IllegalStateException@29576} "java.lang.IllegalStateException: [RLM_ERR_SCHEMA_VALIDATION_FAILED]: Schema validation failed due to the following errors: Property ‘PelletInsertionVisit.pelletMapAsList’ of type ‘array’ has unknown object type ‘PelletToNumberUsedMapEntry’

I have multiple classes but the ones germane to this are the following:

class PelletInsertionVisit : RealmObject { @PrimaryKey var _id: String = "" var visitDate = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) var pelletsUsed: RealmList<Pellet> = realmListOf() var pelletMapAsList: RealmList<PelletToNumberUsedMapEntry> = realmListOf() //Maps pellet name to number of pellets of that type used var signedInUser: String = "UNKNOWN" //User name of user performing the update override fun toString(): String { return "PelletInsertionVisit(visitDate=$visitDate, pelletsUsed=$pelletsUsed, pelletMap=$pelletMapAsList, signedInUserUserName='$signedInUser')" } }

And this one:

class PelletToNumberUsedMapEntry : EmbeddedRealmObject { var pelletKey: String = "NOTSET" var numberUsed: Int = 0 }

Right before I call the Realm.open(it) I execute this code:

private fun setupRealmSync() { val user = runBlocking { appServiceInstance.currentUser } val config = user?.let { SyncConfiguration .Builder( it, setOf(UserAccount::class,Pellet::class, PelletInsertionVisit::class, PelletLot::class, Patient::class, Practice::class ) ) .initialSubscriptions(rerunOnOpen = true) { realms -> add( realms.query<PelletInsertionVisit>(), name = "Pellet Insertion Visit info", updateExisting = true ) add(realms.query<PelletLot>(), name = "Pellet info", updateExisting = true) add(realms.query<Patient>(), name = "Patient info", updateExisting = true) add(realms.query<Practice>(), name = "Practice info", updateExisting = true) add(realms.query<UserAccount>(), name = "UserAccount info", updateExisting = true) } .build() }

So, PelletInsertionVisit is added (and I see it in MongoDB Compass) but it’s not liking the other class. I thought that it being embedded would suffice but is that the issue?

Hi @Darrin_Smith1 ,

Yes, it is: PelletToNumberUsedMapEntry is still a class, if you declare for the SyncConfiguration a setOf classes, you should include the embedded ones as well.

For others that follow, what I needed to do is add the embedded class to my builder like this:

private fun setupRealmSync() { val user = runBlocking { appServiceInstance.currentUser } val config = user?.let { SyncConfiguration .Builder( it, setOf(UserAccount::class,Pellet::class, PelletToNumberUsedMapEntry::class, PelletInsertionVisit::class, PelletLot::class, Patient::class, Practice::class ) )

That did it!