Realm ORM: how to deal with cards?

I am creating an Android application and I need to save Map<String,MyClass>. I just started using Realm ORM , as it supports one-to-one and one-to-many lists, enumerations and lists. I also found a workaround for string lists (i.e. I need to create a StringWrapper class that encapsulates a string. However, from the documentation, I understand that there is no easy way RealmMaphow this works for lists. So, I'm looking for a better way to save the map. My current idea is to replace my map with a list of KeyValueObjectencapsulating objects String(the old map key) and MyClasssimilarly Map.Entry, is there any other solution that does not require me to remake the domain model for technological reasons?

+4
source share
2 answers

As you noticed, Realm does not yet support maps: https://github.com/realm/realm-java/issues/759

You can use model classes:

class MyData extends RealmObject {
    private RealmList<MyMap> myMap;
}

class MyMap extends RealmObject {
    private String key;
    private MyClass value;
}

Suppose you have an object MyDatawith a name MyDataand you want to get the value associated with myKey, a query may be useful MyClass myClass = myData.getMyMap().where().equalTo("key", myKey).firstFirst().

+16
source

Well, if you have the luxury of remaking the persistence layer a bit, then the easiest solution is not to use Map<Stringat all.

A Map<Stringliterally means that a particular line is associated with a single object. You can do the same by simply including this line in your RealmObject. You can then query based on this.

, RealmResults` , .

+1

Source: https://habr.com/ru/post/1628091/


All Articles