NSPredicate on multiple to-many relationships in Core Data

Viewed 1388

I have an NSPredicate which includes multiple aggregate filters, which is throwing an exception.

I have the following core data model:

Core data model

I want to pick those ApparelItems for which any of the colours has an rgb of 13576743, and for which all of the picks have a pickTime earlier than a given NSDate.

My code to create the predicate is:

let request = NSFetchRequest(entityName: "ApparelItem")
var predicates = [NSPredicate]()

predicates.append(NSPredicate(format: "ANY colours.rgb = 13576743"))

// find the NSDate representing midnight x days ago
let cal = NSCalendar.currentCalendar()
if let xDaysAgo = cal.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: [])
{
    let midnightXDaysAgo = cal.startOfDayForDate(xDaysAgo)

    predicates.append(NSPredicate(format: "(ALL picks.pickTime < %@)", midnightXDaysAgo))
}

request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)

let searchData = try? objectContext.executeFetchRequest(request)

I get the following exception:

Exception name=NSInvalidArgumentException, reason=Unsupported predicate (ANY colours.rgb == 13576743) AND ALL picks.pickTime < CAST(479347200.000000, "NSDate")

I have tried:

  1. Each individual predicate works fine. Ie ANY colours.rgb = ... works, also ALL picks.pickTime < ... works. They just don't work when combined into the same query.

  2. Combining the two using into a single query, linked with AND, rather than using NSCompoundPredicate. Result is the same.

Is it possible that core data simply doesn't support filtering on more than one to-many relationship? That would seem odd. In which case how should I do the above?

2 Answers
Related