Reputation: 2214
I need to filter a many to many relationship. This means I have a third table for this connection.
Event <<- EventPlayerState ->> Player
I tried to find out if a Event already has the player connected via EventPlayerState
.
The EventPlayerState
table contains the attribute isActive
to indicate if a player is active in the event.
I accessed the events relationship property hasEventPlayerStates
. And tried to filter it with a NSPredicate
. But it always returns false.
NSPredicate *seachPlayerPredicate = [NSPredicate predicateWithFormat:@"(hasPlayer = %@)", [player objectID]];
NSSet *playerEventStates = [event hasPlayerEventStates];
NSSet *filteredPlayer = [playerEventStates filteredSetUsingPredicate:seachPlayerPredicate];
if([filteredPlayer count] == 1) {
return YES;
}
return NO;
What is wrong? Or is there a better way to do it?
Upvotes: 1
Views: 103
Reputation: 80265
If you are not storing any additional information in your join table, that table to connect the two entities it is superfluous. You should then only have a many-to-many relationship between Player
and Event
.
The predicate is also much simpler:
[NSPredicate predicateWithFormat:@"players.@count = 0"]
or to check for a particular Event
event.players.count == 0
In case you do need the join table, e.g because you are storing a timestamp or other information, it still looks sort of the same from the Event
's point of view.
The corresponding checks are exactly equivalent:
[NSPredicate predicateWithFormat:@"playerStates.@count = 0"]
event.playerStates.count == 0
Upvotes: 2