views:

61

answers:

1

Hi everyone, Let say I have an entity user which has a one to many relationship with the entity menu which has a one to many relationship with the entity meal which has a many to one relationship with the entity recipe which has a one to many relationship with the entity element. What I would like to do is to select the elements which belong to a particular user (username = myUsername) and particular menu*s* (minDate < menu.date < maxDate).

Does anyone have an idea how to get them?

Thanks

+1  A: 

You want to do a fetch on the element entity. From your description, it's not clear if you've defined the inverse relationships1 (e.g. element to recipie) and whether they are to-many or to-one. Assuming you have them defined and they are to-one, you can do your fetch with a predicate like [2]:

[NSPredicate predicateWithFormat:@"%@ > recipe.meal.menu.date && recipie.meal.menu.date < %@ && recipie.meal.menu.user.username LIKE[cd] %@", minDate, maxDate, myUsername];

I've used LIKE[cd] for a case-insensitive and diacritic-insensitive comparison on usename. Also note that Core Data stores dates as a double NSTimeInterval, without time-zone information. If you want to do timezone-sensitive date comparison, you're in for much more work.

1 If you haven't defined the inverse relationships, do it. As has been said many times elsewhere, Core Data is a graph management framework that just happens to be able to persist its graph to disk. It will do a lot of behind the scenes work to maintain referential integrity for you, automatically, if you define the inverse relationships. Particularly for many-to-many relationships, Core Data virtually requires the inverse to make things work.

[2] If you have defined to-many inverses where I've assumed to-one, you'll have to use a slightly more complicated predicate. The first clause, for example, would be

@"ANY (@unionOfSets.recipe.meal.menu.date) < %@"

(this last bit is untested; the KVO set operators are always a bit of experimenting for me). You can read up on the set and array KVO operators.

Barry Wark
Thanks... it (the second case) is pretty much what I'm looking for! I have a one to many relationship between recipe and meal. What I don't understand is the @unionOfSets. What does it do? Does it 'merge' the to many relationships (in my case the meals)? Or do I have to write: [email protected]?
ncohen
Oh I forgot... some of my elements don't have a relationship with recipe! So when I try this: @unionOfSets.recipe.meal.menu.date I get an error! How can I get round of this?
ncohen
You can read up on @unionOfSets at http://developer.apple.com/mac/library/documentation/cocoa/conceptual/KeyValueCoding/Concepts/ArrayOperators.html. Yes, you may need `recipie.@unionOfSets`.
Barry Wark
@ncohen, do you mean some elements don't have any recipe instance set on the recipe relation or that you have multiple element entities, some of which don't have a recipe relation at all? In the first case, you're fine. In the second case, you can't perform the query as described.
Barry Wark