Counting CoreData entries based on relationship entities

As I continue working on the new product management app, I was trying to get a quick CoreData count. To be precise,  I needed to get the count for how many of its relationships met a given criteria. There’s quite a few approaches but none as clean as “the filter”

Screen Shot 2021 08 22 at 4 49 59 PM

Using it for boolean arguments:

      entity.childOfEntity?.filter({ ($0 as! entityType).booleanAgument != true}).count

Using the image example above:

      noteObject.tasksOfNote?.filter({ ($0 as! Task).completed != true}).count

Using it for string arguments:

      entity.childOfEntity?.filter({ ($0 as! entityType).stringArgument != ”Something}).count

Definitely recommended it for most CoreData relationship work, simplifies things quite a bit. 

 

Marc