I am using NSFetchedResultsController
to display data in a table view. I have 2 objects, Client, and Formula. A Client
can have many Formulas
but a Formula can have just one Client
, so a one-to-many relationship. When I tap on a certain Client
in a table view cell
, I want to only be seeing formulas related to that client, but when I tap on other cells, those contain the same Formula objects, so it's not filtering. I know I need to use a predicate to filter the fetch request
, but I do not know where to start.
This is the fetchedResultsController
I have set up.
let fetchedResultsController: NSFetchedResultsController<Formula> = {
let fetchRequest: NSFetchRequest<Formula> = Formula.fetchRequest()
let sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
fetchRequest.sortDescriptors = sortDescriptors
return NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: CoreDataStack.context, sectionNameKeyPath: "date", cacheName: nil)
}()
So somewhere in there, I have to add fetchRequest.predicate = NSPredicate(format: ...)
but I do not know what to enter inside that initializer.