Octoshape
Octoshape

Reputation: 1141

Searching an NSSet attribute in CoreData with a searchbar

I have a CoreData model with an object "bar" which has a to-many relationship "drinks" to the entity "drink".

The entity drink has only one attribute called "name".

Now in my app I have a tableView that shows all the bars and has a searchBar included.

In this searchBar I would like to be able to search for the name, location and the possible drinks that are available in a bar.

I have already implemented the name and the location like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"
                                       name contains[cd] %@ OR
                       location.name_extern contains[cd] %@ OR 
                       location.name_intern contains[cd] %@", 
                            searchText, searchText, searchText];

So now my question is: How can I search for the name attribute in the NSSet of drinks?

EDIT:

To make it more clear, here are the important data models:

Bar

Attributes: 
name
location
drinks

Location

Attributes:
name_extern
name_intern

Drink:

Attributes:
name

I have a searchText and need to check the following 4 places whether it matches something:

  1. name (of the Bar)
  2. name_extern (of the Location of the Bar)
  3. name_intern (of the Location of the Bar)
  4. name (of ALL the drinks of the Bar)

Upvotes: 3

Views: 1236

Answers (1)

Ashley Mills
Ashley Mills

Reputation: 53102

Something like this should work:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"
                                       name contains[cd] %@ OR
                       location.name_extern contains[cd] %@ OR 
                       location.name_intern contains[cd] %@ OR
                   ANY location.drinks.name contains[cd] %@", 
                            searchText, searchText, searchText, searchText];

Upvotes: 4

Related Questions