java - How to store enums in Realm? -
how store java enum classes when using realm?
from documentation, seems realm yet support storing enums:
field types realm supports following field types: boolean, byte, short, ìnt, long, float, double, string, date , byte[]. integer types byte, short, int, , long mapped same type (long actually) within realm. moreover, subclasses of realmobject , realmlist supported model relationships.
there similar question asked objective-c , got answered here. none yet java though.
without custom methods unfortunately cumbersome @ moment, can store string representation instead , convert to/from enum.
public enum foo { foo } // v1: using static methods public class bar1 extends realmobject { private string enumvalue; // getters/setters // static methods handle enum values public static foo getenum(bar1 obj) { return foo.valueof(obj.getenumvalue()) } public static foo setenum(bar1 obj, foo enum) { return obj.setenumvalue(enum.tostring()); } } // v2: use dummy @ignore field create getters/setters can override yourself. public class bar2 extends realmobject { private string enumvalue; // dummy field @ignore private string enum; public void setenumvalue(string enumvalue) { this.enumvalue = enumvalue; } public string getenumvalue() { return enumvalue; } public void setenum(foo foo) { setenumvalue(foo.tostring()); } public foo getenum() { return foo.valueof(getenumvalue()); } }
Comments
Post a Comment