java Realm 支持枚举吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37997766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:01:11  来源:igfitidea点击:

Enums support with Realm?

javaandroidenumsrealm

提问by MJB22

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 postthat Realm doesn't support enum yet.

我正在开发一个 android 应用程序和 Realm,我需要为我的一个对象创建一个枚举属性;但我在这篇文章中发现 Realm 还不支持枚举。

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:

我的枚举类(ShuttleTypes)对应于:

HOME = 1;  

and

WORK = 2;

Can anybody suggest me how to do it?

有人可以建议我怎么做吗?

回答by Christian Melchior

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

您可以使用问题中描述的模式: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:

基本上将其保存为 Realm 中的 String 并将其转换为输入和输出:

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);
  }
}

回答by Nicolás Carrasco

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

如果您需要一个适用于 Kotlin 的解决方案,您可以使用以下方法:

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

MyEnumis the enum declared in @ChristianMelchior answer.

MyEnum是@ChristianMelchior 答案中声明的枚举。

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

值得一提的是,由于enum没有后台字段,所以不会持久化到Realm中。不需要在上面使用@Ignore注解

回答by Eric

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

我创建了一个 Kotlin 委托,这意味着更少的重复

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)
    }
}