New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums support #776

Open
Egorand opened this Issue Jan 21, 2015 · 44 comments

Comments

Projects
None yet
@Egorand

Egorand commented Jan 21, 2015

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

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Jan 21, 2015

Contributor

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.

Contributor

cmelchior commented Jan 21, 2015

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.

@Egorand

This comment has been minimized.

Show comment
Hide comment
@Egorand

Egorand Jan 21, 2015

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 commented Jan 21, 2015

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.

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Jan 21, 2015

Contributor

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.

Contributor

cmelchior commented Jan 21, 2015

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.

@mpost

This comment has been minimized.

Show comment
Hide comment
@mpost

mpost Jan 25, 2015

I would also like to see enum support.

mpost commented Jan 25, 2015

I would also like to see enum support.

@cmelchior cmelchior added P2 and removed P2 labels Apr 15, 2015

@cmelchior cmelchior changed the title from Enums support to Enums https://github.com/realm/realm-java/pull/954 Apr 15, 2015

@cmelchior cmelchior changed the title from Enums https://github.com/realm/realm-java/pull/954 to Enums Apr 15, 2015

@cmelchior cmelchior changed the title from Enums to Enums support Apr 15, 2015

@bmunkholm bmunkholm added P2 and removed P1 labels Apr 27, 2015

@bmunkholm bmunkholm added T:Feature and removed T:Enhancement labels Jun 13, 2015

@hamen

This comment has been minimized.

Show comment
Hide comment
@hamen

hamen commented Aug 13, 2015

+1

@frazer-rbsn

This comment has been minimized.

Show comment
Hide comment
@frazer-rbsn

frazer-rbsn commented Aug 16, 2015

+1!

@ganlaw

This comment has been minimized.

Show comment
Hide comment
@ganlaw

ganlaw commented Aug 24, 2015

+1

@rgrinberg

This comment has been minimized.

Show comment
Hide comment
@rgrinberg

rgrinberg commented Sep 5, 2015

+1

@saket

This comment has been minimized.

Show comment
Hide comment
@saket

saket commented Sep 6, 2015

+1

@AKlimashevskyCedon

This comment has been minimized.

Show comment
Hide comment
@AKlimashevskyCedon

AKlimashevskyCedon Sep 11, 2015

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

AKlimashevskyCedon commented Sep 11, 2015

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

@StErMi

This comment has been minimized.

Show comment
Hide comment
@StErMi

StErMi Sep 25, 2015

Any updates?

StErMi commented Sep 25, 2015

Any updates?

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Sep 25, 2015

Contributor

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.

Contributor

cmelchior commented Sep 25, 2015

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.

@benjamincombes

This comment has been minimized.

Show comment
Hide comment
@benjamincombes

benjamincombes commented Nov 10, 2015

+1

@benjamincombes

This comment has been minimized.

Show comment
Hide comment
@benjamincombes

benjamincombes Nov 10, 2015

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 commented Nov 10, 2015

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

This comment has been minimized.

Show comment
Hide comment
@mateusgrb

mateusgrb commented Dec 21, 2015

+1

@paulpv

This comment has been minimized.

Show comment
Hide comment
@paulpv

paulpv commented Jan 8, 2016

+1

@musesum

This comment has been minimized.

Show comment
Hide comment
@musesum

musesum commented Jan 14, 2016

+1

@doncorsean

This comment has been minimized.

Show comment
Hide comment
@doncorsean

doncorsean commented Jan 26, 2016

+1

@mehrad-rafigh

This comment has been minimized.

Show comment
Hide comment
@mehrad-rafigh

mehrad-rafigh commented Feb 10, 2016

+1

@PaulWoitaschek

This comment has been minimized.

Show comment
Hide comment
@PaulWoitaschek

PaulWoitaschek Apr 18, 2016

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

PaulWoitaschek commented Apr 18, 2016

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

@oscarg798

This comment has been minimized.

Show comment
Hide comment
@oscarg798

oscarg798 commented Apr 27, 2016

+1

@mateusgrb

This comment has been minimized.

Show comment
Hide comment
@mateusgrb

mateusgrb Apr 27, 2016

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

mateusgrb commented Apr 27, 2016

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

@n-belokopytov

This comment has been minimized.

Show comment
Hide comment
@n-belokopytov

n-belokopytov commented Jun 9, 2016

+1

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Jun 24, 2016

Contributor

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

Contributor

cmelchior commented Jun 24, 2016

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

@romshiri

This comment has been minimized.

Show comment
Hide comment
@romshiri

romshiri commented Jul 5, 2016

+1

@nicomlas

This comment has been minimized.

Show comment
Hide comment
@nicomlas

nicomlas commented Aug 18, 2016

+1

@mateusgrb

This comment has been minimized.

Show comment
Hide comment
@mateusgrb

mateusgrb Nov 11, 2016

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.

mateusgrb commented Nov 11, 2016

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.

@Zhuinden

This comment has been minimized.

Show comment
Hide comment
@Zhuinden

Zhuinden Nov 11, 2016

Contributor

otherwise wouldn't be able to override the field in ContactRealm later.

?

Contributor

Zhuinden commented Nov 11, 2016

otherwise wouldn't be able to override the field in ContactRealm later.

?

@mateusgrb

This comment has been minimized.

Show comment
Hide comment
@mateusgrb

mateusgrb Nov 12, 2016

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.

mateusgrb commented Nov 12, 2016

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.

@powder366

This comment has been minimized.

Show comment
Hide comment
@powder366

powder366 commented Jan 21, 2017

+1

@mptrista

This comment has been minimized.

Show comment
Hide comment
@mptrista

mptrista commented Jan 23, 2017

+1

@apouche

This comment has been minimized.

Show comment
Hide comment
@apouche

apouche Feb 14, 2017

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.

apouche commented Feb 14, 2017

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.

@bmunkholm bmunkholm added S:Backlog and removed S:P2 Backlog labels May 18, 2017

@bmunkholm bmunkholm removed the S:Backlog label Aug 8, 2017

@YuriDenison

This comment has been minimized.

Show comment
Hide comment
@YuriDenison

YuriDenison commented Nov 8, 2017

+1

@Zhuinden

This comment has been minimized.

Show comment
Hide comment
@Zhuinden

Zhuinden Nov 8, 2017

Contributor

I think this is a subset of #1694 .

Contributor

Zhuinden commented Nov 8, 2017

I think this is a subset of #1694 .

@cmelchior

This comment has been minimized.

Show comment
Hide comment
@cmelchior

cmelchior Nov 8, 2017

Contributor

Yes, it will most likely be implemented that way since the concept of a Java enum doesn't easily transfer to other platforms.

Contributor

cmelchior commented Nov 8, 2017

Yes, it will most likely be implemented that way since the concept of a Java enum doesn't easily transfer to other platforms.

@eygraber

This comment has been minimized.

Show comment
Hide comment
@eygraber

eygraber Nov 29, 2017

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?

eygraber commented Nov 29, 2017

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?

@PerfectedTech

This comment has been minimized.

Show comment
Hide comment
@PerfectedTech

PerfectedTech commented Feb 6, 2018

+1

@cfxway

This comment has been minimized.

Show comment
Hide comment
@cfxway

cfxway commented Feb 26, 2018

+1

@bmunkholm bmunkholm added the O:User label May 30, 2018

@roberhofer roberhofer self-assigned this Jun 14, 2018

@blocha

This comment has been minimized.

Show comment
Hide comment
@blocha

blocha Jun 27, 2018

still no support? after so many years?

blocha commented Jun 27, 2018

still no support? after so many years?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment