0

I would like to have an object that is an ordered collection of multiple different kinds of Realm Objects, like so...

public class One: Object {
    dynamic var name = ""
}

public class Two: Object {
    dynamic var label = ""
}

public class Listing: Object {
    let onesAndTwos = List<Object>()
}

Is there an elegant way to do this?

I know I can add an Enum-like wrapper object...

public class OneOrTwo: Object {
    dynamic var one: One?
    dynamic var two: Two?
}

public class Listing: Object {
    let onesAndTwos = List<OneOrTwo>()
}

But I'd like to avoid that indirection, if possible.

Aneel
  • 1,403
  • 1
  • 17
  • 22
  • I don't know much about realm, but if it's based on a database table, then no, probably not. What exactly are you trying to achieve? – Alexander Apr 10 '17 at 01:35
  • I'm trying to make a list of steps in a process where there are different kinds of steps. Each kind of step stores different information. – Aneel Apr 10 '17 at 01:37
  • 1
    This is a good place to start: http://stackoverflow.com/q/45621/3141234 – Alexander Apr 10 '17 at 01:37
  • 1
    Thanks, I understand how to do this in a table-based database. It's not clear to me whether realm *is* table-based, or is a object-graph datastore, where this would be possible. – Aneel Apr 10 '17 at 01:43
  • Yeah, I would be interested to see the answers this question yields – Alexander Apr 10 '17 at 01:45
  • Just guessing, based on names, but there's a DataType called Link, that seems to define the origin and target tables on a per-Link basis, not a per-column basis... https://github.com/realm/realm-core/blob/master/src/realm/link_view.hpp – Aneel Apr 10 '17 at 01:57

1 Answers1

0

List<T> property can't contain objects of different types.

Please learn more in docs at https://realm.io/docs/swift/latest/#relationships and https://realm.io/docs/swift/latest/#model-inheritance, that should explain why it's not possible.

So I suggest you to use composition over inheritance, basically the same way as you mentioned in your post.

Dmitry
  • 7,300
  • 6
  • 32
  • 55