Моя модель данных
![enter image description here](https://i.stack.imgur.com/NNSKq.png)
У Клиента может быть много Оценок, но у Оценки может быть только один Клиент в качестве обратной связи. Моя цель - получить все Оценки, которые есть у Клиента, а также отфильтровать их по свойству firstName первоначального Клиента.
Моя функция фильтра:
func filterData(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let words = textInSearchField.components(separatedBy: " ")
for word in words{
if (word).count == 0{
continue
}
let firstNamePredicate = NSPredicate(format: "firstName contains[c] %@", word)
let lastNamePredicate = NSPredicate(format: "lastName contains[c] %@", word)
let idPredicate = NSPredicate(format: "id contains[c] %@", word)
// I am using a Compound Predicate so that I can filter by first name, last name but also the id property.
let orPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: [firstNamePredicate, lastNamePredicate, idPredicate])
clientsEntity.predicate = orPredicate
// results is an array of type [NSManagedObject], I am using the compound predicate I created above to fetch specific clients.
results = try! context.fetch(clientsEntity) as! [NSManagedObject]
}
}
По сути, я хочу получить все оценки клиента, отфильтрованные по firstName, lastName или ID. Является ли NSPredicate лучшим подходом?
Спасибо.