user1904273
user1904273

Reputation: 4764

IOS/Xcode/search/coredata: Can a Table View or Search Bar display from more than one entity when using Core Data?

Is there a way to have a search bar that searches more than one entity (i.e. table in Database Speak) and displays results? For example, search for and display both authors and book titles with a certain string in them?

Note I would like to have this search bar on top of a table that only pulls from one entity. The search should be more general in what it pulls than the specific table i.e. if you are looking at books pulled from a books entity (or table), could you search by both title and author in the search box above?

Upvotes: 1

Views: 286

Answers (1)

Mundi
Mundi

Reputation: 80271

Quite simple, really. Assuming every book has one author, use a search predicate for the book entity similar to the following:

NSPredicate(format: "author.name CONTAINS[cd] %@ || title CONTAINS[cd] %@", 
            searchTerm, searchTerm)

Upvotes: 0

Related Questions