Realm ORM: how to deal with Maps?

后端 未结 2 1800
-上瘾入骨i
-上瘾入骨i 2021-01-02 04:14

I am creating an Android app and I need to persist a Map. I\'ve just started to use Realm ORM, as it supports one-to-one and one-to-many,

2条回答
  •  -上瘾入骨i
    2021-01-02 05:10

    As you notice, Realm doesn't yet support maps: https://github.com/realm/realm-java/issues/759

    You could use the model classes:

    class MyData extends RealmObject {
        private RealmList myMap;
    }
    
    class MyMapEntry extends RealmObject {
        private String key;
        private MyClass value;
    }
    

    Say you have a MyData object called myData and you wish to fetch the value associated with myKey, the query MyClass myClass = myData.getMyMap().where().equalTo("key", myKey).firstFirst() might be useful.

提交回复
热议问题