创建RealmConfiguration时,是否存在与Swift中的objectTypes参数等效的Java? https://realm.io/docs/swift/latest/api/Classes/Realm/Configuration.html
我正在移植一个IOS应用程序,该应用程序使用多个Realm数据存储区来将只读应用程序数据与读/写用户数据分开,因此,我需要指定哪些模型属于哪个 Realm ,以避免在RealmMigrationNeededException时无法在其中找到模型。第一个 Realm 实际上属于第二个 Realm 。
编辑:
好的,当我从RealmObject扩展一个新模型(TLGalleryItem)并将其保存在第二个 Realm 实例中时,出现以下异常。如果我删除了对模型的所有引用并删除了对模型的RealmObject的继承,则异常会消失。只要从RealmObject扩展模型(代码中仍然没有引用),我就会得到:
04-23 08:43:35.603 26777-26777/com.(my project name).android.(my project name) E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.(my project name).android.(my project name), PID: 26777
java.lang.ExceptionInInitializerError
at com.(my project name).android.(my project name).activities.insightFinder.ThemesActivity.<init>(ThemesActivity.kt:23)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Cannot open the read only Realm. 'TLGalleryItem' is missing.
at io.realm.Realm.<init>(Realm.java:179)
at io.realm.Realm.createInstance(Realm.java:435)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getInstance(Realm.java:364)
at com.(my project name).android.(my project name).data.DBService.<clinit>(DBService.kt:21)
at com.(my project name).android.(my project name).activities.insightFinder.ThemesActivity.<init>(ThemesActivity.kt:23)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
最佳答案
这回答了我的问题:
https://stackoverflow.com/a/46609808/8902232
相关信息是:使用RealmConfiguration中的modules(...)。对我来说,在 Kotlin ,这看起来像:
private val shippedRealmConfig = RealmConfiguration.Builder()
.name(Realm.DEFAULT_REALM_NAME)
.assetFile("shippedData.realm")
.schemaVersion(1)
.modules(ShippedModule())
.readOnly()
.build()
private val shippedRealm = Realm.getInstance(shippedRealmConfig)
@RealmModule(classes = arrayOf(TLTheme::class, TLSymptom::class, TLUnhelpfulBelief::class, TLInsight::class))
private class ShippedModule