Denise Carpenter
Denise Carpenter

Reputation: 41

Relationships using Realm.io with Swift

I have leagues that have teams that have players. A player can belong to different teams and leagues. A team can belong to different leagues. I am having trouble setting these relationships with Realm.

class League: RLMObject
{
    dynamic var name = "name"

    dynamic var teams = RLMArray(objectClassName: Team.className())
}


class Team: RLMObject
{
    dynamic var name = "name"

    var leagues: [League] {
        return linkingObjectsOfClass("League", forProperty: "teams") as [League]
    }

    dynamic var players = RLMArray(objectClassName: Player.className())
}


class Player: RLMObject
{
    dynamic var name = "name"

    var teams: [Team] {
        return linkingObjectsOfClass("Team", forProperty: "players") as [Team]
    }

}

Upvotes: 2

Views: 353

Answers (1)

Denise Carpenter
Denise Carpenter

Reputation: 41

The relationships are correct, it was the way I was presenting the data on the view that was the problem. So this is an example of the correct way of representing these relationships.

Upvotes: 2

Related Questions