realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Enums support

Open Egorand opened this issue 10 years ago • 47 comments

I was hoping enums are supported, as they're a better alternative to ints for enumerations.

Egorand avatar Jan 21 '15 17:01 Egorand

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.

cmelchior avatar Jan 21 '15 19:01 cmelchior

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.

Egorand avatar Jan 21 '15 22:01 Egorand

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.

cmelchior avatar Jan 21 '15 23:01 cmelchior

I would also like to see enum support.

mpost avatar Jan 25 '15 10:01 mpost

+1

hamen avatar Aug 13 '15 13:08 hamen

+1!

frazer-rbsn avatar Aug 16 '15 19:08 frazer-rbsn

+1

rgrinberg avatar Sep 05 '15 15:09 rgrinberg

+1

saket avatar Sep 06 '15 09:09 saket

is it possible to help you to speed up enum support?

AKlimashevskyCedon avatar Sep 11 '15 15:09 AKlimashevskyCedon

Any updates?

StErMi avatar Sep 25 '15 07:09 StErMi

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.

cmelchior avatar Sep 25 '15 07:09 cmelchior

+1

benjamincombes avatar Nov 10 '15 13:11 benjamincombes

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

benjamincombes avatar Nov 10 '15 14:11 benjamincombes

+1

mateusgrb avatar Dec 21 '15 15:12 mateusgrb

+1

paulpv avatar Jan 08 '16 23:01 paulpv

+1

musesum avatar Jan 14 '16 16:01 musesum

+1

doncorsean avatar Jan 26 '16 19:01 doncorsean

+1

mehrad-rafigh avatar Feb 10 '16 13:02 mehrad-rafigh

+1

ddszczygiel avatar Feb 19 '16 12:02 ddszczygiel

+1

Queatz avatar Feb 28 '16 22:02 Queatz

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

cmelchior avatar Feb 29 '16 10:02 cmelchior

+1

ghost avatar Mar 28 '16 01:03 ghost

+1

njovy avatar Mar 29 '16 04:03 njovy

@cmelchior Use #Enum.name() instead of toString() as its final.

PaulWoitaschek avatar Apr 18 '16 08:04 PaulWoitaschek

+1

oscarg798 avatar Apr 27 '16 18:04 oscarg798

Guys, you can now react to issues/comments with a thumbs up. No more need to comment "+1" =)

mateusgrb avatar Apr 27 '16 18:04 mateusgrb

+1

n-belokopytov avatar Jun 09 '16 13:06 n-belokopytov

@ashk3156 Please create another issue than reusing this one, and please add the code that actually throw the NullPointer as well.

cmelchior avatar Jun 24 '16 12:06 cmelchior

+1

romshiri avatar Jul 05 '16 12:07 romshiri

+1

nicomlas avatar Aug 18 '16 09:08 nicomlas