Reputation: 12752
I have an array of year/months tuples:
[(2015, 11), (2015, 12), (2016, 1), (2016, 2), (2016, 3), (2016, 4), (2016, 5), (2016, 6), (2016, 7), (2016, 8), (2016, 9)]
In my CoreData ManagedObjectModel, I have a MonthlyReport entity that has year
and month
properties (Int16)
I'm trying to figure out an efficient way to fetch all models which year and month properties match the tuples from the array.
I tried the following (with uniqueYears
and uniqueMonths
arrays of unique values):
let predicateYear = NSPredicate(format: "$name in $NAME_LIST", "year", uniqueYears)
let predicateMonths = NSPredicate(format: "$name in $NAME_LIST", "month", uniqueMonths)
let fetchRequest = NSFetchRequest(entityName: "EntityName")
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateYear, predicateMonths])
but that doesn't work; the app crashes with the message unimplemented SQL generation for predicate : ($name IN $NAME_LIST)'
Right now, I'm stuck with fetching then manually filtering all models, which is far from efficient.
Any suggestions or pointers would be appreciated
Upvotes: 1
Views: 169
Reputation: 46728
There is not an easy way to do this. You will need to build a fairly complex predicate to find the combinations. Something like this:
var predicate: NSPredicate
for tuple in incomingTuples {
let singlePredicate = NSPredicate("year == %@ AND $month == %@", tuple.0, tuple.1)
if predicate == nil {
predicate = singlePredicate
} else {
predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicate, singlePredicate])
}
}
I suspect that this is going to be an expensive SQL query.
Upvotes: 2