I'm using the following code to fetch all the data in "category".
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"category")
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
How do I only brings categories where the "type" is equal to "products"?
To "filter" results in Core Data, use NSPredicate
like so:
let filter = "products"
let predicate = NSPredicate(format: "type = %@", filter)
fetchRequest.predicate = predicate
So, in the below example
private let context = (NSApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let filter = id
let predicate = NSPredicate(format: "id = %@", filter)
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"TodoListItem")
fetchRequest.predicate = predicate
do{
let fetchedResults = try context.fetch(fetchRequest) as! [NSManagedObject]
print("Fetch results")
if let task = fetchedResults.first as? TodoListItem{
print(task)
}
// try context.save()
}catch let err{
print("Error in updating",err)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With