24

I'm working on an android app and Realm, and I need to create an enum attribute for one of my objects; but I discovered in this post that Realm doesn't support enum yet.

My object is like this:

public class ShuttleOption extends RealmObject {
    private int Id;
    private String Label;
    private ShuttleTypes OriginShuttleType;
}

and my enum class (ShuttleTypes) corresponds with:

HOME = 1;  

and

WORK = 2;

Can anybody suggest me how to do it?

MJB22
  • 358
  • 1
  • 3
  • 10

3 Answers3

40

You can use the pattern described in the issue: https://github.com/realm/realm-java/issues/776#issuecomment-190147079

Basically save it as a String in Realm and convert it going in and out:

public enum MyEnum {
  FOO, BAR;
}

public class Foo extends RealmObject {
  private String enumDescription;

  public void saveEnum(MyEnum val) {
    this.enumDescription = val.toString();
  }

  public MyEnum getEnum() {
    return MyEnum.valueOf(enumDescription);
  }
}
Sagar
  • 3,159
  • 3
  • 29
  • 28
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
14

If you need a solution that works on Kotlin you can use the following:

open class Foo: RealmObject() {
    var enum: MyEnum
        get() { return MyEnum.valueOf(enumDescription) }
        set(newMyEum) { enumDescription = newMyEnum.name }
    private var enumDescription: String = MyEnum.FOO.name
}

MyEnum is the enum declared in @ChristianMelchior answer.

It is worth mentioning that since enum doesn't have a backing field,it won't be persisted into Realm. There is no need to use the @Ignore annotation on it

1

i created a Kotlin delegate, which means a little less repitition

usage:

open class SomeDbModel : RealmObject() {

    @delegate:Ignore
    var variableEnum: MyEnum by enum(::variable)
    private var variable: String = MyEnum.Default.name
}

delegate implementation:

package com.github.ericytsang

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty

inline fun <R, reified T : Enum<T>> enum(
    backingField: KMutableProperty0<Int>
) = OrdinalToEnumDelegate<R, T>(T::class, backingField)

val <T : Enum<T>> KClass<out T>.enumValues get() = java.enumConstants!!.toList()

class StringToEnumDelegate<R, T : Enum<T>>(

    /**
     * enum class to convert the ordinal values in [backingField] to.
     */
    enumClass: KClass<T>,

    /**
     * the property containing [T]'s ordinal value.
     */
    private val backingField: KMutableProperty0<String>

) : ReadWriteProperty<R, T> {

    private val enumValues = enumClass.enumValues.associateBy { it.name }

    override fun getValue(thisRef: R, property: KProperty<*>): T {
        return enumValues[backingField.get()]
            ?: error("no corresponding enum found for ${backingField.get()} in ${enumValues.keys}")
    }

    override fun setValue(thisRef: R, property: KProperty<*>, value: T) {
        backingField.set(value.name)
    }
}
Eric
  • 16,397
  • 8
  • 68
  • 76