https://kotlinlang.org logo
#android
Title
# android
m

moizest89

07/23/2019, 3:27 PM
Hello Guys!! Someone has experience using Realm with
sealed
class like values? I’ve found examples with
Enum
class but not with sealed 😕
h

henrikhorbovyi

07/23/2019, 4:26 PM
What you mean:
sealed
+ Realm ?
If you mean: Use
sealed class
to control data flow, handle errors, etc, you can use it with any ORM. If you understand the core of sealed classes.
Well, you could explain your situation and we may try to help you 🙂
d

Darrinps

07/23/2019, 5:48 PM
Yes, some code please. Have worked a good bit with Realm.
Here is what I've done for an enum. enum class EventType { START, STOP, RESET } open class StopwatchEvent: RealmObject() { var positionOccurance = 0L var eventTypeAsInt = 0 //Given Realm isn't great with enums, just store as int fun getEventType() : EventType { when(eventTypeAsInt) { 1 -> return EventType.START 2 -> return EventType.STOP 3 -> return EventType.RESET else -> { Timber.w("Unknown event type saved: $eventTypeAsInt. Defaulting to START") return EventType.START } } } }
h

henrikhorbovyi

07/23/2019, 6:52 PM
image.png
The same example above could be done (using
sealed classes
) like this ☝️
But as we said; Share with us your problem
m

moizest89

07/23/2019, 8:44 PM
🤔
Yes, the example is clear
I mean, in data model create a field of type
EventType
(sealed class example above ) and try to save after in Realm but I guess is not a good option xD
something like this:
because I need to put ChallengeType manually
z

zhuinden

08/29/2019, 5:15 PM
You need to save a value type that Realm supports (string, int, something like that) then map it to the type that you're returning. If the sealed class has arguments though... not sure how you'd save that, i guess you could serialize it as String
m

moizest89

08/30/2019, 3:07 PM
@zhuinden finally I did something like you say ^_^
92 Views