Home > Software engineering >  When using Core Data "Transformable" attribute type to get a set of any primitive type, ho
When using Core Data "Transformable" attribute type to get a set of any primitive type, ho

Time:08-26

I use a Transformable attribute named viewingDates on a Item entity.

Core Data attribute "viewingDates"

Thus I can declare the property @NSManaged public var viewingDates: Set<Date> on the Item class.

But how to format a NSPredicate to fetch all Item where viewingDates is not empty?

Note: My database contains more than 15000 items and I would like if possible to avoid having to fetch them all before filtering them via items.filter { !$0.viewingDates.isEmpty }.

CodePudding user response:

With transformable properties you can't filter fetch requests based on the property values, at all. Transformable properties are converted to Data when you save, and back when you fetch, but during fetch they're still Data. All you can really do is check whether they're null.

If this property were optional, you could check if it was null. But you cannot use a predicate that considers the contents of the property.

  • Related