realm-kotlin
realm-kotlin copied to clipboard
RealmAny doesn't support dynamically checking RealmObject data
Currently, we only support RealmAny.asRealmObject<DynamicRealmObject>() for dynamic realms. This makes it problematic to retrieve RealmAny values where the type might be uncertain.
Obviously, the workaround could be to have a second field:
class Person: RealmObject {
var mixed: RealmAny? = null
var type: String = "Child"
}
fun test(person: Person) {
val clazz = when(person.type) {
"Child" -> Child::class,
else -> TODO()
}
val child: Child = person.mixed!!.asRealmObject(clazz)
// Do things with the child
}
But maybe it would make more sense to be able to use our dynamic capabilities directly, i.e.:
class Person: RealmObject {
var mixed: RealmAny? = null
}
fun test(person: Person) {
val childType: DynamicRealmObject = person.mixed!!.asRealmObject<DynamicRealmObject>().type
val clazz = when(childType) {
"Child" -> Child::class,
else -> TODO()
}
val child = person.mixed!!.asRealmObject(clazz)
// Do things with the child
}
In particular, deleting an object probably doesn't require knowing the type, ie.: MutableRealm.delete(DynamicRealmObject).