Scott Nightingale
Scott Nightingale

Reputation: 21

realmconfiguration objecttype java

Is there a Java equivalent to the objectTypes parameter in Swift, when creating a RealmConfiguration?https://realm.io/docs/swift/latest/api/Classes/Realm/Configuration.html

I'm porting an IOS app that uses multiple Realm datastores to separate read only application data from read/write user data, and hence I need to specify which models belong to which realm to avoid a RealmMigrationNeededException when realm can't find a model in the first realm which actually belongs to the second.

EDIT:

Ok so the below exception crops up when I extended a new model (TLGalleryItem) from RealmObject which is to be saved in the second realm instance. If I remove all references to the model and remove inheritance from RealmObject on the model exception goes away. As soon as I extend the model from RealmObject (still no references to it in the code) I get this:

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) 

Upvotes: 1

Views: 247

Answers (2)

MattyG
MattyG

Reputation: 8497

I can't say how to do it on Android, but it's not usually necessary to set the objectTypes of your Realm configurations in iOS. I personally use it if I have two or more Realm configurations, as more of a safe coding practice. If I accidentally call the wrong Realm configuration, which doesn't contain my desired objects, instead of getting no results, I get a helpful debug message from Realm: "Object type xxxxx is not managed by the Realm."

I suspect that your issue is caused by something else. There should be a more detailed debug log with the 'RealmMigrationNeededException' error. What does it say?

Remember that during development, if you change a Realm model (and don't have migration set up for that), you will need to delete the Realm somehow before building and running again. On iOS, we delete the app from the device or simulator.

Edit:

Interesting findings. I see why you are thinking down the objectTypes route. Sanity checks:

  • Are your configurations being set before any calls to Realm? Realm.setDefaultConfiguration() is not thread-safe.
  • Are your calls to Realm using the correct configuration.

Take a look at this answer for more clues.

Upvotes: 1

Scott Nightingale
Scott Nightingale

Reputation: 21

this answers my question: https://stackoverflow.com/a/46609808/8902232

the relevant info is: use modules(...) in the RealmConfiguration. For me in Kotlin this looked like:

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

Upvotes: 1

Related Questions