Hello Guys!! Someone has experience using Realm wi...
# android
m
Hello Guys!! Someone has experience using Realm with
sealed
class like values? I’ve found examples with
Enum
class but not with sealed 😕
h
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
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
image.png
The same example above could be done (using
sealed classes
) like this ☝️
But as we said; Share with us your problem
m
🤔
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
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
@zhuinden finally I did something like you say ^_^
114 Views