Skip to content
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 · 47 comments
Open

Enums support #776

Egorand opened this issue Jan 21, 2015 · 47 comments

Comments

@Egorand
Copy link

Egorand commented Jan 21, 2015

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

@cmelchior
Copy link
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.

@Egorand
Copy link
Author

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
Copy link
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.

@mpost
Copy link

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 Enums support Enums https://github.com/realm/realm-java/pull/954 Apr 15, 2015
@cmelchior cmelchior changed the title Enums https://github.com/realm/realm-java/pull/954 Enums Apr 15, 2015
@cmelchior cmelchior changed the title Enums Enums support Apr 15, 2015
@bmunkholm bmunkholm added P2 and removed P1 labels Apr 27, 2015
@hamen
Copy link

hamen commented Aug 13, 2015

+1

@frazer-rbsn
Copy link

+1!

@rgrinberg
Copy link

+1

1 similar comment
@saket
Copy link

saket commented Sep 6, 2015

+1

@AKlimashevskyCedon
Copy link

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

@StErMi
Copy link

StErMi commented Sep 25, 2015

Any updates?

@cmelchior
Copy link
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.

@benjamincombes
Copy link

+1

@benjamincombes
Copy link

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
Copy link

+1

5 similar comments
@paulpv
Copy link

paulpv commented Jan 8, 2016

+1

@musesum
Copy link

musesum commented Jan 14, 2016

+1

@doncorsean
Copy link

+1

@mehrad-rafigh
Copy link

+1

@ddszczygiel
Copy link

+1

@Zhuinden
Copy link
Contributor

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

?

@mateusgrb
Copy link

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
Copy link

+1

@mptrista
Copy link

+1

@cmelchior cmelchior added Blocked This issue is blocked by another issue Design-Required labels Jan 30, 2017
@apouche
Copy link

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.

@YuriDenison
Copy link

+1

@Zhuinden
Copy link
Contributor

Zhuinden commented Nov 8, 2017

I think this is a subset of #1694 .

@cmelchior
Copy link
Contributor

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

@eygraber
Copy link

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?

@PerfectedApp
Copy link

+1

1 similar comment
@cfxway
Copy link

cfxway commented Feb 26, 2018

+1

@roberhofer roberhofer self-assigned this Jun 14, 2018
@blocha
Copy link

blocha commented Jun 27, 2018

still no support? after so many years?

@shreshtha18
Copy link

how to store list of enum in realm android?

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 9, 2019

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.

@RealmBot RealmBot removed the Blocked This issue is blocked by another issue label Sep 24, 2020
@shaqaruden
Copy link

How is this still not supported?

@Ashkan-san
Copy link

2024 and still no real enum support?

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

No branches or pull requests