【トラブル】【Android】Realm に関するトラブル

■ はじめに

https://dk521123.hatenablog.com/entry/2018/09/01/213649
https://dk521123.hatenablog.com/entry/2018/09/02/130212
https://dk521123.hatenablog.com/entry/2020/07/20/232009

などで扱った Realm  に関するトラブルシューティングに関して
まとめる

目次

【1】エラー「RealmMigrationNeededException」が発生する
【2】エラー「"xxxxx" of type "yyyy" is not supported」が発生

【1】エラー「RealmMigrationNeededException」が発生する

エラー内容

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: zzz.zzz.zzzz, PID: 3362
    java.lang.RuntimeException: Unable to start activity ComponentInfo{zzz.zzz.zzzz/zzz.zzz.zzzz.MainActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'Xxxx.xxxxx' has been added.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
     Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'Xxxx.xxxxx' has been added.
        at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
        ... 10 more

原因

* データ変更したため

解決案

* 多分、いくつかあると思うが、、、

その1:データ変更したら削除してやり直す
https://teratail.com/questions/52444

// 修正前
Realm.init(this)
var realm = Realm.getDefaultInstance()

// 修正後
Realm.init(this)
var realmConfiguration =
    RealmConfiguration.Builder()
        .deleteRealmIfMigrationNeeded()
        .build()
var realm = Realm.getInstance(realmConfiguration)

【2】エラー「"xxxxx" of type "yyyy" is not supported」が発生

エラー内容

"xxxxx" of type "java.util.Calendar" is not supported.

原因

* データ型でサポート外のものもあるので注意

https://realm.io/jp/docs/java/latest/

より抜粋
~~~~
String, Integer, Boolean, Float, Double, Short, Long, Byte, ByteArray and Date
~~~~

解決案

* サポート内のデータ型で置き換える
 => 今回は「Calendar」→「Date」

関連記事

DB を使用する ~ Realm 編 ~
https://dk521123.hatenablog.com/entry/2018/09/01/213649
画面コンポーネント / ListView ~ Realm データを表示 ~
https://dk521123.hatenablog.com/entry/2018/09/02/130212
Kotlin / Realm で英単語帳を作る
https://dk521123.hatenablog.com/entry/2020/07/20/232009
Android でのトラブル その1
https://dk521123.hatenablog.com/entry/2020/07/28/000000
プロジェクト作成時のトラブル
https://dk521123.hatenablog.com/entry/2018/08/31/165700
エミュレータ に関するトラブル
https://dk521123.hatenablog.com/entry/2013/09/20/232756
レイアウトで表示されるエラー / 警告について
https://dk521123.hatenablog.com/entry/2013/10/13/231530
Android 開発時のトラブル ~ Eclipse編 ~
https://dk521123.hatenablog.com/entry/2013/09/29/121726