realm-java
realm-java copied to clipboard
Enums support
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
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