Inheritance with Realm, confusion


#1

Hello guys!

I have an issue about Inheritance with my Objects in Realm.

Could you please have a look a it. I have :

  • an Object Activity
  • an Object Sport which I want to be a subclass of Activity
  • an Object Seminar which I want to be a subclass of Activity

To make this happen I write, according to the documentation, the following code :

// Base Model
class Activity: Object {
      dynamic var id = ""
      dynamic var date = NSDate() 	

     override static func primaryKey() -> String? {
        return "id"
     }
}

// Models composed with Activity
class Nutrition: Object {
    dynamic var activity: Activity? = nil
    dynamic var quantity = 0
}

  class Sport: Object {
    dynamic var activity: Activity? = nil
    dynamic var quantity = 0
    dynamic var duration = 0
}

Now I have an Model Category which I want it to hold the activities, doesn’t matter if it’s an Nutrition or Sport.

Here is my code :

class Categorie: Object {
    
    let activities = List<Activitie>()
    dynamic var categoryType: String = ""
    
    override static func primaryKey() -> String? {
        return "categoryType"
    }

}

Now I try to add a Nutrition object to my List<Activitie> by doing this :

let nutrition =  Nutrition(value: [ "activity": [ "date": NSDate(), "id": "0" ], "quantity": 12 ])

try! realm.write {
     realm.add(nutrition, update: true)
}

It doesn’t work because List<Activitie> expect an Activity Object and not a Nutrition Object. Where am I wrong ?

Thanks a lot for the help.

Looking forward to keep on working with Realm.


List of multiple object types?
#2

Looks like you also posted this question on StackOverflow

Let me know if you have any clarifying questions about the answer

-Ian


#3

As Yoam Farges pointed it out in StackOverflow , Realm doesn’t support :

Casting between polymorphic classes (subclass->subclass, subclass->parent, parent->subclass, etc.).

Which is what I was trying to do.

You guys are right when saying that my Inheritance is not an Inheritance, but as you can see in the Realm documentation it’s how you achieve it.

Thanks to the informations I got in this github post I could achieved what I wanted and could keep a easy readability.

Use an option type for polymorphic relationships :

class PolyActivity: Object {
    dynamic var nutrition: Nutrition? = nil
    dynamic var sport: Sport? = nil
    dynamic var id = ""

    override static func primaryKey() -> String? {
        return "id"
    }
}

Create my main Object Activity

class Activity: Object {
    dynamic var date = NSDate()
}

and have my Nutrition and Sport object inherited properly to Activity

class Nutrition: Activity {
    dynamic var quantity = 0
}
class Sport: Activity {
    dynamic var quantity = 0
    dynamic var duration = 0
}

My Category object can now hold a List

class Categorie: Object {
    let activities = List<PolyActivity>()
    var categoryType: String = ""

    override static func primaryKey() -> String? {
        return "categoryType"
    }
}

And this is how I create my Nutrition object :

let polyActivity = PolyActivity(value : [ "id": primaryKey ] )
poly.nutrition = Nutrition(value: [ "date": NSDate(), "quantity": 0, "duration": 0 ])

let category = Category(value: ["categoryType" : "0"])
category.activities.append(polyActivity)

And to retrieve just use Optional Binding :

if let nutrition = category.activities[0].nutrition { }

If you guys have a better, clearer, easier solution please go head !