Question

I have an Core Data entity called Album; an album has a relationship to Song entities (called songs), and each Song has an arbitrary count of Tag entities (in a tags property), tags have a name. Now I want to check whether any of the songs in an Album contain a Tag; using a single NSPredicate.

An ideas that came to mind was:

[NSPredicate predicateWithFormat: @"ANY (ANY songs).tags LIKE %@", someTagName];

But this causes an error at runtime ("Unable to parse the format string"). I tried various variations, without success.

Is there any way to do this?

Was it helpful?

Solution

I think you want

[NSPredicate predicateWithFormat:@"ANY songs.@unionOfSets.tags LIKE %@", someTagName]

Check out the Key-Value Programming Guide section on set and array operators.

OTHER TIPS

Just a thought, but perhaps LIKE is not available on the iPhone? I know I had a hard time with BETWEEN, which doesn't appear to work.

So a tag does not have a many-to-many relationship with songs? If it was modeled that way, getting the songs for a tag would just involve accessing the property tag.songs. The way you have it modeled, it seems that tags can be duplicated for each song (which may be just fine in your app).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top