e-Jah
e-Jah

Reputation: 335

Accessing many-to-many relationships in Core Data

I am using Core Data in my iPhone app.

I have 2 Entities: List and Item, linked by a many-to-many relationship. The relationship name in List is items and in Item is lists.

I want to get all the items from the lists selected by users.

Right now I am trying to build a predicate which look like that:

NSPredicate *basePredicate = [NSPredicate predicateWithFormat:@"lists IN %@", listsIds];

listsIds is an Array of ObjectIDs

But when I run the program I get this error:

unimplemented SQL generation for predicate : (lists IN {0x594ac10 })'

Any idea how to do it?

Thank you!

Upvotes: 0

Views: 854

Answers (2)

TechZen
TechZen

Reputation: 64428

Do you even need a predicate?

If the users select List objects then to find the related Item objects you just walk the List.items relationship.

Suppose you have an array of List object chosen by the user. To get an array of related Item objects just use:

NSArray *itemArray=[listArray valueForKey:@"items"];

Once you have one object in one side of a relationship you can find all the other objects without having to fetch.

Upvotes: 1

David
David

Reputation: 7303

Try this:

NSPredicate *basePredicate = [NSPredicate predicateWithFormat:
                             @"ALL list.objectID IN %@", listsIds];

Not sure if you need the ALL so you can also try:

NSPredicate *basePredicate = [NSPredicate predicateWithFormat:
                             @"list.objectID IN %@", listsIds];

Upvotes: 0

Related Questions