views:

113

answers:

1

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.

+2  A: 

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:

  1. Bind the Book array controller's filter predicate to a new predicate property "bookFilterPredicate" in your app delegate.
  2. Add outlets for the Author and Genre array controllers.
  3. Observe changes to the selectedObjects properties of both array controllers.
  4. When either changes, update the filter predicate property like this: self.bookFilterPredicate = [NSPredicate predicateWithFormat:@"author IN %@ && genre IN %@",authorArrayController.selectedObjects,genreArrayController.selectedObjects];
gerry3
Whoops, I was toying with different examples to explain the problem as simply as possible, and ended up being inconsistent.I should have been more clear - I understand that i'll have to apply a predicate to filter the array controller, but am unsure of how to set up this relationship in IB, if it is even possible, or if i'll have to fall back to writing some glue to tie it together?
Scott
I *think* you have to use code since I don't see any way to pull in information from any (and you will pull from two) array controllers in the filter predicate box.
gerry3
I suspected as much, but the reference in the apple doco threw me off! That looks great, cheers.
Scott