How to keep transfers in the kingdom?

How to store Java enumeration classes when using Realm ?

From the documentation , it seems that Realm does not yet support saving enums:

Field Types Realm supports the following field types: boolean , byte , short , รฌnt , long , float , double , String , Date and byte [] . The integer byte types, short, int, and long, are mapped to the same type (actually actually) within Realm. Moreover, subclasses of RealmObject and RealmList are supported for modeling relationships.

There is a similar question that was asked for Objective-C and got the answer here . There is no Java yet.

+5
source share
1 answer

Without custom methods, unfortunately now is a bit cumbersome, but instead you can save the string representation and convert it to / from an enumeration.

public enum Foo { FOO } // V1: Using static methods public class Bar1 extends RealmObject { private String enumValue; // Getters/setters // Static methods to handle the 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 a dummy @Ignore field to create getters/setters you 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()); } } 
+6
source

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


All Articles