I have this fetch results controller I'm using to fetch data into a view. The fetch controller does manage to fetch managedObjects but the problem is that it's fetching every "Recipe" entity. I would like it to only fetch recipes that are connected to the category I specified. I think my predicate might be wrong but I dont know how to fix it. Is there anyone who can point me in the right direction? Thanks
// I pass category from a previous view to this view which also sets recipes
var category: FoodCategory? {
didSet {
recipes = category?.recipes
}
}
var recipes: NSSet?
lazy var recipeFetchedResultsController: NSFetchedResultsController = { () -> NSFetchedResultsController<NSFetchRequestResult> in
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Recipe")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
// is this correct???
let hasCategory = NSPredicate(format: "name = %@", category!.name!)
(self.recipes)?.filter({ (hasCategory) -> Bool in
(self.recipes?.contains(category))!
})
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.managedObjectContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
return frc
}()