mardi 30 décembre 2014

Fetch Core Data entity with a filtered set of child elements based on a property

I'm developing my first application with CoreData and I'm struggling with a common problem I think.


Let's say I have an entity Playlist which has a many to many relationship with another entity Song and also Video. So my Playlist object looks like this



class Playlist : NSManagedObject {
var songs: NSSet
var videos: NSSet
}


Song and Video entities both have a Boolean field "removed" that I use to track which song or video has been removed from the playlist. I later call my remote API to remove it on server's side but this is for offline purpose.


What I want is to retrieve from CoreData a Playlist object with its list of songs and videos that are not removed (so those with removed = false).


One solution I see would be to get all the elements by simply fetching on the Entity Playlist and then filter out manually but I'd like to know if I can do this more elegantly using Core Data.




Aucun commentaire:

Enregistrer un commentaire