开发者

Core Data Predicates with Subclassed NSManagedObjects

I have an AUDIO class. This audio has a SOUND_A subclass and a SOUND_B subclass. This is all done correctly and is working fine.

I have another model, lets call it PLAYLIST_JOIN, and this can contain (in the real world) SOUND_A's and SOUND_B's, so we give it a relationship of AUDIO and PLAYLIST.

This all works in the app.

The problem I am having now is querying the PLAYLIST_JOIN table with an NSPredicate. What I want to do is find an exact PLAYLIST_JOIN item by giving it 2 keys in th开发者_运维问答e predicate

sound_a._sound_a_id = %@ && playlist.playlist_id = %@

and

sound_b.sound_b_id = %@ && playlist.playlist_id = %@

The main problem is that because the table does not store sound_a and sound_b, but stored audio, I cannot use this syntax. I do not have the option of reorganizing the sound_a and sound_b to use the same _id attribute name, so how do I do this?

Can I pass a method to the predicate? something like this:

[audio getID] = %@ && playlist_id = %@


It gets a little complicated but you need to add a third condition to the predicate:

(entity.name = sound_a && _sound_a_id = %@ && playlist.playlist_id = %@) && (entity.name = sound_b && sound_b_id = %@ && playlist.playlist_id = %@)

This is assuming you are querying against the audio abstract and telling it to return subclasses. Because the condition is checked left to right, if the first condition fails it will move on and not throw errors because _sound_a_id does not exist.

The first condition is referencing the NSEntityDescription that is a part of the NSManagedObject and its name attribute is just a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜