Question

for a turn-based multiplayer game I'm working on, I defined a Game entity to store game information. Each game is player between two registered Users. Since I already had a User entity I decided to link the users to the game using a relationship. I defined a player1 and a player2 relationship from the Game to the User entity because I want to explicitly assign each user to one of the two player slots (instead of a to-many players relationship where the players are part of a set). I also have a relationship games from the User entity to the Game entity to store all the games that a user is currently involved in.

I can't set an inverse relationship this way, because then my games relationship would have to have an inverse of either player1 or player2. But Xcode is complaining about the lack of inverse, so I'm wondering if I should model my relationships in another way? Here are the Xcode warnings:

Consistency Error: User.games does not have an inverse; this is an advanced setting (no object can be in multiple destinations for a specific relationship)

and

Misconfigured Property: Game.player1 should have an inverse

Was it helpful?

Solution

You can solve that by defining two inverse relationships: games1 as inverse to player1, and games2 as inverse to player2.

Defining inverse relationships is generally recommended, as it helps to keep the integrity of the object graph. For example, if a user is deleted and the inverse relationship delete rule is set to "Nullify", then the player1/player2 entry in all related games is set to NULL and does not point to non-existent user objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top