I was hoping enums are supported, as they're a better alternative to ints for enumerations.
Hi @Egorand
Enums are on our wishlist as well, but there are some challenges involved as one of the primary design goals of Realm is cross-platform compatibility and enums doesn't exist in Objective-C like they do in Java.
Hi @cmelchior and thanks for the response,
Enums can easily be converted into simple ints and back, I think typedefs are the closest feature that Objective-C provides (my knowledge of Objective-C is close to zero). Hence it shouldn't be hard to represent enums internally in a way that both platforms could work with it.
True, but that would be very brittle with regard to modifying the enums afterwards. See here for a discussion on the topic: http://stackoverflow.com/questions/229856/ways-to-save-enums-in-database.
It is possible to find a solution, but right now we want to prioritise other features.
I would also like to see enum support.
+1
+1!
+1
+1
+1
is it possible to help you to speed up enum support?
Any updates?
Right now we are focusing on other bigger features (custom methods, null, async queries and migration API). However once we open up for custom methods it will be possible to work around this by doing the conversion yourself in the getter/setter.
That said we still want to add support for it, but proper support will take a bit longer.
+1
I don't know if it can help anybody, but here is what I use as a workaround :
public enum CampaignStatus {
LIVE,
UPCOMING
}
@SerializedName("status")
private String statusRaw;
@Ignore
private transient CampaignStatus status;
public CampaignStatus getStatus() {
return CampaignStatus.valueOf(getStatusRaw().toUpperCase());
}
public void setStatus(CampaignStatus status) {
setStatusRaw(status.name());
}
+1
+1
+1
+1
+1
+1
+1
Note that with #2196 now merged to master (will be part of 0.88), it is possible to support Enums using the following pattern:
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 (enumDescription != null) ? MyEnum.valueOf(enumDescription) : null;
}
}
+1
+1
@cmelchior
Use #Enum.name() instead of toString() as its final.
+1
Guys, you can now react to issues/comments with a thumbs up. No more need to comment "+1" =)
+1
@ashk3156 Please create another issue than reusing this one, and please add the code that actually throw the NullPointer as well.
+1
+1
Reporting a case where lack of enums support made things less pretty: in Kotlin, I created an interface Contact and a class ContactRealm implementing that interface. The idea was to use Contact wherever possible and keep its ~implementation~ decoupled, so that later I could plug a ContactORM implementing same interface, for example. Contact has a field 'sex' which would be best represented as an enum but due to Realm's limitation I had to declare it as a String in the first place, otherwise wouldn't be able to override the field in ContactRealm later.
otherwise wouldn't be able to override the field in ContactRealm later.
?
interface Contact {
var name: String
var age: Int
var pictureUrl: String
var sex: String
enum class Sex {
MALE, FEMALE
}
}
open class ContactRealm(
override var name: String = "",
override var age: Int = 0,
override var pictureUrl: String = "",
override var sex: String = "") : RealmObject(), Contact
@Zhuinden the field sexin Contact can't be a Sex, as it needs to be overridden in ContactRealm and Realm doesn't support enums.
+1
+1
I ran into this thread while trying to solve the same problem. Here was my approach to this in kotlin
```
open class Contract: RealmModel {
enum class PaymentMethod { CREDIT_CARD, WIRE_TRANSFER }
private var _paymentMethod: String? = null
var paymentMethod: PaymentMethod?
get() {
if (_paymentMethod != null)
return PaymentMethod.valueOf(_paymentMethod!!)
else
return null
}
set(value) {
val realm = Realm.getDefaultInstance()
realm.executeTransaction {
_paymentMethod = value.toString()
}
realm.close()
}
}
````
basically I used a private string property taken as a string to construct a computed property with that enum correspondance. Not sure this is very "realm" friendly since I'm new to Android development but I thought this worked actually pretty well.
+1
I think this is a subset of https://github.com/realm/realm-java/issues/1694 .
Yes, it will most likely be implemented that way since the concept of a Java enum doesn't easily transfer to other platforms.
I investigated ObjectBox today (don't worry; I'm sticking with Realm 馃槣 ), and came up with something like this for getting enums supported. Would something like that work here?
+1
+1
still no support? after so many years?
how to store list of enum in realm android?
RealmList<String> although then it becomes non-queryable as per https://github.com/realm/realm-object-store/issues/513 so it should probably be combined to a comma separated list in a single String value.
Most helpful comment
Note that with #2196 now merged to master (will be part of 0.88), it is possible to support Enums using the following pattern: