我有几个简单的 RealmObject 类,如下所示:
class Patient : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var firstName: String = ""
var lastName: String = ""
var visits: RealmList<PelletInsertionVisit> = realmListOf()
var lastFourOfSSN: String = ""
}
class PelletInsertionVisit : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId()
var visitDate = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
var pelletsUsed: RealmList<Pellet> = realmListOf()
}
class Pellet: EmbeddedRealmObject {
var size = PelletSize.ZERO.value
var lotNumber: String = "UNKNOWN"
}
当我执行此代码时,打开会引发异常:
fun openPatientRealm() : Realm {
val configuration = RealmConfiguration.create(schema = setOf(Patient::class))
Logger.d("About to open Realm Configuration: $configuration")
try {
val realm = Realm.open(configuration) //EXCEPTION HERE
Logger.d("Realm opened: : ${realm.toString()}")
return realm
}
catch (ex: Exception) {
Logger.e("Exception caught: " + ex.message)
Logger.e("Stack trace: " + ex.stackTraceToString())
}
throw Exception("Realm Could Not be Opened")
}
例外情况是:
[RLM_ERR_SCHEMA_VALIDATION_FAILED]:架构验证由于以下错误而失败:
请问我做错了什么?在我看来,就像文档说它应该使用 EmbeddedRealmObject 但我遗漏了一些东西。
问题出在配置上。长话短说,您必须告诉它您打算使用的所有类。所以,它去了:
val configuration = RealmConfiguration.create(schema = setOf(Patient::class, Pellet::class, PelletInsertionVisit::class, PelletLot::class))