I’m trying to implement something similar to the iTunes browser, to browser a simple database of Books. I have the following entities – Author, Genre and Book. I would like to display an author list and a genre list, which act to filter the main Book list.
I have tried doing this in 2 different ways – modeled as:
Author ( has many ) Genres ( has many ) Books
…with multiple instances of the same Genre so each author has their own for a given genre name, I have a nice drill-down hierarchy to display in my table views (albeit a bit illogical to duplicate Genres). However, when I select multiple Authors, I end up displaying dupes of the same Genre, because they are, in fact, distinct objects.
So, I tried doing it, more sensibly, with these relationships:
Author ( has many ) Books Book ( has one ) Genre
I can get the Genre array by taking the distinct union of Genre’s in the current selected Author(s) book array, but now i’m left with the problem of filtering the book list displayed based on the selected Genre(s). Because the Genre’s are shared, I can’t just use CurrentGenre.books, or I lose the selected Author filtering. I have noticed the ‘filter predicate’ field in interface builder, available on the object controllers, but am stuck working out how to actually use it to apply the selected Genre as a filter to an episode list. The apple documentation says:
“You can type a predicate directly
into the predicate editor text field
in the inspector panel of Interface
Builder or you can set it
programmatically using
setFetchPredicate:.
which gives me the impression i’m on the right track, but that’s about the end of it. I’m trying to lock down the model in a nice Cocoa-esque fashion now, so as to minimize ‘glue code’ bits and changes later down the track. It seems like a fairly simple problem I should be able to sort out graphically in IB, but so far it’s eluded me!
Thanks in advance.
I am a bit confused by your introducing “episodes” in the middle of the discussion, but I will assume you just mean “books” still.
You are definitely on the right track. You want a data model like this:
Author <–>> Book
Genre <–>> Book
Or maybe even:
Author <<–>> Book (if you support reference books, etc)
Genre <<–>> Book (if you want multi-genre support)
Once a user has selected author(s) and genre(s) you will want a Book array controller to use a filter predicate that only shows books with those author(s) or genre(s).
UPDATE
This should work:
selectedObjectsproperties of both array controllers.self.bookFilterPredicate = [NSPredicate predicateWithFormat:@"author IN %@ && genre IN %@",authorArrayController.selectedObjects,genreArrayController.selectedObjects];