Hi!
I've been poking around realm library as an internal storage for data in android and it works great. Lately I've been wondering whether or not you could replace shared app preferences (later: SP) with this particular storage as in my experience shared preferences tend to accept data only once and never let you to either clear or edit them. I am aware that if database is in app's internal storage, it can only be accessed by that particular app.
So far I've been using SP to store encoded cookies from server, which acted as a hash map. Meanwhile migrating to realm I've come to an issue that field type java.util.HashMap<java.util.String, java.util.String> is not supported. How come? Do all fields need to extend RealmObject or be primitive?
Supported fields are mentioned by the docs:
boolean, byte, short, int, long, float, double, String, Date and byte[].
Moreover, subclasses of RealmObject and RealmList extends RealmObject> are supported to model relationships.
HashMap is not a RealmList, not a RealmObject, not String, not byte[], and not a primitive.
Although it is worth noting that generics aren't well-supported by the Realm transformer (there is no such thing as T.class and stuff).
But the values for a given key in a String, String map in reality is a RealmResults<Something> results = realm.where(Something.class).equalTo("stringField", stringValue).findAll(); if you model the key as a String in the field (and if one value can belong to only one key)
Yes, Maps and Sets are not yet supported, but it is on our roadmap. You can track the feature here: https://github.com/realm/realm-java/issues/759
In your particular use case, you could probably model it as a Configuration object though, and use Realm's queries instead of a hashmap:
public class Configuration extends RealmObject {
@PrimaryKey
private String id; // your encoded cookie
// Your configuration
private boolean foo;
private String bar;
private int baz;
}
// Find a specific configuration
Config config = realm.where(Configuration.class).equalTo("id", encodedCookie).findFirst();
In the mean time I'll close this as a duplicate as maps support is tracked in #759
During the response time I already worked around it by making a class which extended RealmObject and wrapped it in RealmList.
Glad to know that HashMap support is on the way. Thank you for quick responses.
This issue is still here.. please someone suggest any solution
A Map<String, String> field is a String field of a RealmObject.
Map your data into a compatible format.
But I can't use another, because I am getting dynamic key, value in JSOn from Server.
Ex. JSON :
result : {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : "value4",
"key5" : "value5",
}
And to parse them I need to use Map only, as I don't know which key is gonna com from server side. So GSON suggest to use Map only.
Realm doesn't support Maps yet. If the keys are not a well-defined format, your best alternative is probably two RealmLists, one with keys and one with values.
ok I will check that. thanks
Most helpful comment
During the response time I already worked around it by making a class which extended RealmObject and wrapped it in RealmList.
Glad to know that HashMap support is on the way. Thank you for quick responses.