GangstaGraham
GangstaGraham

Reputation: 9355

NSPredicate Compare Two Sets And Many-To-Many Relationship

Two model classes

AUser
- name

AGroup
- users 

Groups and Users share a many-to-many relationship.

I wish to check whether two sets share the exact same users (not more users or less users)

But unfortunately I am getting the following error: to-many key not allowed here

Note that I have looked at other SO questions but none seemed to fit because I have tried to use their methodologies for the past 2 hours, but I could not get it to work. Or at least I did not understand them very well.

Also is my predicate alright for comparing two mutable sets?

- (BOOL)conversationExists:(NSMutableSet *)members {
    NSFetchRequest *request= [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"AGroup" inManagedObjectContext:_managedObjectContext];
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"users==%@",members]; 
    //There is more code that I have not shown here as it is irrelevant to the question
    //Mainly The NSPredicate line is the problem
}

Thank you so much for your time. I really appreciate it.

From, New iOS programmer

Upvotes: 1

Views: 1048

Answers (2)

tc.
tc.

Reputation: 33592

Why does it need to be a predicate?

What's wrong with [[groups valueForKey:@"users"] isEqualToSet:members]?

Upvotes: 2

Patrick Tescher
Patrick Tescher

Reputation: 3447

This post had an answer: How to use the "ALL" aggregate operation in a NSPredicate to filter a CoreData-based collection

Essentially something like this:

[NSPredicate predicateWithFormat:@"SUBQUERY(members, $member, $member IN %@).@count = %d", members, [members count]];

Upvotes: 2

Related Questions